Menu
Topics Index
...
`


Datatypes > Primtive Dataypes > Literals >
Siva Nookala - 23 Aug 2016
Integer literals are used to initialize integer group datatypes like byte, short, int and long. These literals are nothing but a sequence of digits. If they are suffixed with L or l, they are long literals. They are prefixed with zero '0' for octal, '0b' for binary and '0x' for hexa decimal formats. If there is no prefix then it is decimal format. e.g., 53, 189, 211012L, 1199l, 0351, 0x12c2a4d2, 0x12c2a4d2L, 0352l, 0b10011101

We can specify the integers either in decimal, hexadecimal, octal format or binary format. To represent the type as long integer we use L or l as a suffix.
  • We indicate a decimal format by putting the left most digit as nonzero. i.e. it is not prefixed with '0' or '0b' or '0x'.
int decimal = 26; // The number 26, in decimal
  • For hexadecimal format use the prefix '0x', and should be followed by digits 0 to 9 and from a to f.
int hexadecimal = 0x1a;  // The number 26, in hexadecimal
// 0x1a = 16^1 * 1 + 16^0 * 10 = 16 + 10 = 26
  • Octal format should be prefixed with zero '0', and should be followed by the digits 0 to 7.
int octal = 032; // The number 26, in octal
// 032 = 8^1 * 3 + 8^0 * 2 = 24 + 2 = 26
  • We represent binary format as shown below, prefixed with '0b'. This is supported from Java SE 7 (Java 1.7).
byte binary = 0b11010; // The number 26, in binary
// 0b11010 = 2^4 * 1 + 2^3 * 1 + 2^2 * 0 + 2^1 * 1 + 2^0 * 0
// = 16 + 8 + 0 + 2 + 0 = 26
  • For long values suffix it with l or L.
long long1 = 7512884L; // The number 7512884, in decimal
long mc_call_number = 8500622255; // WON'T WORK
long mc_call_number = 8500622255L; // will work

  • For hexa-decimal long values suffix it with l or L and prefix it with 0x.
long hexadecimal_long = 0xab221eeL; // The number 179446254, in hexa-decimal
The below diagram shows the conversion between various formats.
Integer Literals,Learn Java Online,Java Topics,Learning Java Online,Example For Format Conversion Between Various Formats,Conversion Between Various Formats,Number Conversion into Various Format,Integer Literal In Java,Integer Literal Conversion,Number System Conversion,Integer Literals,Primitive Data Type

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App