See §4.2.1 for a general discussion of the integer types and values.
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8):
IntegerLiteral:
DecimalIntegerLiteral
HexIntegerLiteral
OctalIntegerLiteral DecimalIntegerLiteral:
DecimalNumeralIntegerTypeSuffixopt HexIntegerLiteral:
HexNumeralIntegerTypeSuffixopt OctalIntegerLiteral:
OctalNumeralIntegerTypeSuffixopt IntegerTypeSuffix: one of
l L
An integer literal is of type long
if it is suffixed with an ASCII letter L
or l
(ell); otherwise it is of type int
(§4.2.1). The suffix L
is preferred, because the letter l
(ell) is often hard to distinguish from the digit 1
(one).
A decimal numeral is either the single ASCII character 0
, representing the integer zero, or consists of an ASCII digit from 1
to 9
, optionally followed by one or more ASCII digits from 0
to 9
, representing a positive integer:
DecimalNumeral:
0
NonZeroDigit
Digitsopt Digits:
Digit
DigitsDigit Digit:
0
NonZeroDigit NonZeroDigit: one of
1 2 3 4 5 6 7 8 9
A hexadecimal numeral consists of the leading ASCII characters 0x
or 0X
followed by one or more ASCII hexadecimal digits and can represent a positive, zero, or negative integer. Hexadecimal digits with values 10 through 15 are represented by the ASCII letters a
through f
or A
through F
, respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.
HexNumeral:
0 x
HexDigit
0 X
HexDigit
HexNumeral
HexDigit
The following production from §3.3 is repeated here for clarity:
HexDigit: one of
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
An octal numeral consists of an ASCII digit 0
followed by one or more of the ASCII digits 0
through 7
and can represent a positive, zero, or negative integer.
OctalNumeral:
0
OctalDigit
OctalNumeralOctalDigit OctalDigit: one of
0 1 2 3 4 5 6 7
Note that octal numerals are always consist of two or more digits; 0
is always considered to be a decimal numeral-not that it matters much in practice, for the numerals 0
, 00
, and 0x0
all represent exactly the same integer value.
The largest decimal literal of type int
is 2147483648
(). All decimal literals from 0
to 2147483647
may appear anywhere an int
literal may appear, but the literal 2147483648
may appear only as the operand of the unary negation operator -
.
The largest positive hexadecimal and octal literals of type int
are 0x7fffffff
and 017777777777
, respectively, which equal 2147483647
(). The most negative hexadecimal and octal literals of type int
are 0x80000000
and 020000000000
, respectively, each of which represents the decimal value -2147483648
(). The hexadecimal and octal literals 0xffffffff
and 037777777777
, respectively, represent the decimal value -1
.
See also Integer.MIN_VALUE
(§20.7.1) and Integer.MAX_VALUE
(§20.7.2).
A compile-time error occurs if a decimal literal of type int
is larger than 2147483648
(), or if the literal 2147483648
appears anywhere other than as the operand of the unary -
operator, or if a hexadecimal or octal int
literal does not fit in 32 bits.
0 2 0372 0xDadaCafe 1996 0x00FF00FF
The largest decimal literal of type long
is 9223372036854775808L
(). All decimal literals from 0L
to 9223372036854775807L
may appear anywhere a long
literal may appear, but the literal 9223372036854775808L
may appear only as the operand of the unary negation operator -
.
The largest positive hexadecimal and octal literals of type long
are 0x7fffffffffffffffL
and 0777777777777777777777L
, respectively, which equal 9223372036854775807L
(). The literals 0x8000000000000000L
and 01000000000000000000000L
are the most negative long
hexadecimal and octal literals, respectively. Each has the decimal value -9223372036854775808L
(). The hexadecimal and octal literals 0xffffffffffffffffL
and 01777777777777777777777L
, respectively, represent the decimal value -1L
.
See also Long.MIN_VALUE
(§20.8.1) and Long.MAX_VALUE
(§20.8.2).
A compile-time error occurs if a decimal literal of type long
is larger than 9223372036854775808L
(), or if the literal 9223372036854775808L
appears anywhere other than as the operand of the unary -
operator, or if a hexadecimal or octal long
literal does not fit in 64 bits.
0l 0777L 0x100000000L 2147483648L 0xC0B0L