Any constant value which can be assigned to the variable is called literal/constant.

Integer literal

These are whole numbers without any fractional part, Java integer (signed 32 bits) ranges from -2147483648 to 2147483647. They can be specified in decimal, hexadecimal, octal, or binary formats.

int a = 10;     // decimal 10
int b = 0x1A;   // hexadecimal 10
int c = 012;    // octal 10
int d = 0b1101; // binary 10

In Java SE 7 and later, numbers are support underscore ( _ ) to separate groups of digits to improve readability, this is useful for large number especially when representing currency.

int price = 1_000_000;  // equivalent to 1000000

Floating point literal

Floats are numbers with a fractional part, Java float (32 bits) ranges from 1.40239846e-45f to 3.40282347e+38f, and double (64 bits) ranges from 4.94065645841246544e-324 to 1.79769313486231570e+308. They can be specified in decimal or scientific notation.

float c = 10.5f;    // f suffix indicates float instead of double
double e = 10.5;
double f = 1.05e2;  // scientific

Character literal

Character literals represent a single character, enclosed in a pair of single quote (’ ’). Characters as integral literal, which represents the Unicode value or ASCII value of the character, and that integral literal can be specified either in Decimal, Octal, and Hexadecimal forms. But the allowed range is 0 to 65535.

char c = 'a';
char d = 97;      // ASCII value for 'a'
char e = '\u0061' // Unicode value for 'a'

You can perform arithmetic operations on characters, learn more on Arithmetic operation on character.

String literal

These represent a sequence of characters enclosed in double quotes.

String h = "Hello World";

Back to parent page: Java Standard Edition (Java SE) and Java Programming

Web_and_App_DevelopmentProgramming_LanguagesJavaLiteralString_LiteralInteger_LiteralCharacter_LiteralFloating_Point_Literal