For the integral data types we can specify literal value in the following ways.
1) Decimal Literals: It allows 0 to 9 digits.
2) Octal Literals: It allows 0 to 7 digits. Literal value should be prefixed with zero
3) Hexa Decimal literals:
- The allowed digits are 0 to 9 and A to Z.
- This is the one of the very few areas where java is not case sensitive.
- For extra digits both upper case and lower case characters.
- Literal values should be prefixed with ox or 0x.
Valid Declarations:
1) int x=0776; //(valid)
2) int x=0789; //(invalid) Since it is too large
3) int x=0xCOLOUR; //(valid)
4) int x=0xcolour; //(valid)
5) int x=0xColour; //(invalid)
6) int x=0xab1cd; //(valid)
Example:
int a=10;
int b=010;
int c=0x10;
System.out.println(a+”____”+b+”____”+c);
OUTPUT:
10____8____16
By default every integral literals is int type but we can specify explicitly as long type by suffixing with small ‘l” or capital ‘L’.
Example:
int x=10; //(valid)
long l=10L; // (valid)
long l=10; //(valid)
int x=10l; //(invalid)