Java Reference
In-Depth Information
The int Data Type
The int data type is a 32-bit signed Java primitive data type. A variable of the int data type takes 32 bits of memory.
Its valid range is -2,147,483,648 to 2,147,483,647 (-2 31 to 2 31 - 1 ). All whole numbers in this range are known as integer
literals (or integer constants). For example, 10, -200, 0, 30, 19, etc. are integer literals of int . An integer literal can be
assigned to an int variable, say num1 , like so:
int num1 = 21;
Integer literals can also be expressed in
Decimal number format
Octal number format
Hexadecimal number format
Binary number format
When an integer literal starts with a zero and has at least two digits, it is considered to be in the octal number
format. The following line of code assigns a decimal value of 17 (021 in octal) to num1 :
// 021 is in octal number format, not in decimal
int num1 = 021;
The following two lines of code have the same effect of assigning a value of 17 to the variable num1 :
// No leading zero - decimal number format
int num1 = 17;
// Leading zero - octal number format. 021 in octal is the same as 17 in decimal
int num1 = 021;
Be careful when using int literals with a leading zero, because Java will treat these literals as in octal number format.
Note that an int literal in octal format must have at least two digits, and must start with a zero to be treated as an octal
number. The number 0 is treated as zero in decimal number format, and 00 is treated as zero in octal number format.
// Assigns zero to num1, 0 is in the decimal number format
int num1 = 0;
// Assigns zero to num1, 00 is in the octal number format
int num1 = 00;
Note that both 0 and 00 represent the same value, zero. Both have the same effect of assigning a value of zero to
the variable num1 .
All int literals in the hexadecimal number format start with 0x or 0X , that is, zero immediately followed by an
uppercase or lowercase X, and they must contain at least one hexadecimal digit. Note that hexadecimal number
format uses 16 digits, 0-9 and A-F (or a-f ). The case of the letters A to F does not matter. The following are the
examples of using int literals in hexadecimal format:
int num1 = 0x123;
int num2 = 0xdecafe;
int num3 = 0x1A2B;
int num4 = 0X0123;
 
Search WWH ::




Custom Search