Decimal Integer literalssuggest change
Integer literals provide values that can be used where you need a
char instance. (This example focuses on the simple decimal forms. Other examples explain how to literals in octal, hexadecimal and binary, and the use of underscores to improve readability.)
Ordinary integer literals
The simplest and most common form of integer literal is a decimal integer literal. For example:
0 // The decimal number zero (type 'int') 1 // The decimal number one (type 'int') 42 // The decimal number forty two (type 'int')
You need to be careful with leading zeros. A leading zero causes an integer literal to be interpreted as octal not decimal.
077 // This literal actually means 7 x 8 + 7 ... or 63 decimal!
Integer literals are unsigned. If you see something like
+10, these are actually expressions using the unary
\- and unary
The range of integer literals of this form have an intrinsic type of
int, and must fall in the range zero to 231 or 2,147,483,648.
Note that 231 is
1 greater than
Integer.MAX_VALUE. Literals from 0 through to
2147483647 can be used anywhere, but it is a compilation error to use
2147483648 without a preceding unary
\- operator. (In other words, it is reserved for expressing the value of
int max = 2147483647; // OK int min = -2147483648; // OK int tooBig = 2147483648; // ERROR
Long integer literals
Literals of type
long are expressed by adding an
L suffix. For example:
0L // The decimal number zero (type 'long') 1L // The decimal number one (type 'long') 2147483648L // The value of Integer.MAX_VALUE + 1 long big = 2147483648; // ERROR long big2 = 2147483648L; // OK
Note that the distinction between
long literals is significant in other places. For example
int i = 2147483647; long l = i + 1; // Produces a negative value because the operation is // performed using 32 bit arithmetic, and the // addition overflows long l2 = i + 1L; // Produces the (intuitively) correct value.
Reference: JLS 3.10.1 - Integer Literals