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 (-231 to 231 - 1).
All whole numbers in this range are known as integer literals.
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
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:
The following value has no leading zero, and it is a decimal number format.
int num1 = 17;
The following value has leading zero, so it is an octal number format. 021 in octal is the same as 17 in decimal.
int num1 = 021;
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.
All int literals in the hexadecimal number format start with 0x or 0X, and they must contain at least one hexadecimal digit.
The 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;
An int literal can be represented using the binary number format.
All int literals in the binary number format start with 0b or 0B.
The following are examples of using int literals in the binary number format:
int num1 = 0b10101; int num2 = 0b00011; int num3 = 0b10; int num4 = 0b00000010;
Java has a class named Integer, which defines two constants to represent maximum and minimum values for the int data type, Integer.MAX_VALUE and Integer.MIN_VALUE. For example,
int max = Integer.MAX_VALUE; // Assigns maximum int value to max int min = Integer.MIN_VALUE; // Assigns minimum int value to min