- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Integer Literals
Integer literals are used to represent specific integer values. Because integers can be expressed as decimal (base 10), octal (base 8), or hexadecimal (base 16) numbers in Java, each representation has its own form of literal. In addition, integer literals can also be expressed with an uppercase L ('L') or lowercase L ('l') at the end to instruct the compiler to treat the number as a long (64-bit) integer.
As with C and C++, Java identifies any number that begins with a non-zero digit and does not contain a decimal point as a decimal integer literal (for example, any number between 1 and 9). To specify an octal literal, you must precede the number with a leading 0 (for example, 045 is the octal representation of 37 decimal). As with any octal representation, these literals can only contain the numerals 0 through 7. Hexadecimal integer literals are known by their distinctive 'zero-X' at the beginning of the token.
Note - Tokens are the identifiers, keywords, literals, separators, and operators of Java. In short, they are every element of your source code other than whitespace and comments, which are described later in this chapter. Of the token types, only the separators have not been introduced. As the name implies, these tokens serve the purpose of separating other elements of your code. The separators consist of
( ) { } [ ] ; , .
Hex numbers are composed of the numerals 0 through 9 plus the Latin letters A through F (case is not important).
The following shows the largest and smallest values for integer literals in each of the three supported formats:
Largest 32-bit integer literal |
2147483647 |
|
017777777777 |
|
0x7fffffff |
Most negative 32-bit integer literal |
2147483648 |
|
020000000000 |
|
0x80000000 |
Largest 64-bit integer literal |
9223372036854775807L |
|
0777777777777777777777L |
|
0x7fffffffffffffffL |
Most negative 64-bit integer literal |
9223372036854775808L |
|
01000000000000000000000L |
|
0x8000000000000000L |
Caution - Attempts to represent integers outside the range shown in this table using literals result in compile-time errors.