Initialization using Numeric Values- Introduction of Binary Literals in Java
|Literals are constant values in the source code.They do not change their values in the course of the program. Any primitive type of literal can be used in the code like the integer, floating point, character, and Boolean type.
Any whole numeric value is usually treated as an integer literal in Java. In older versions, we have three types of numeric values:
- Decimal: Numbers with a base 10 are known as decimal numbers. They can have both positive as well as negative values. Their declaration is done in the following manner:
byte b=4; short s=15; int i=56; long n=456;
Java does not allow decimal numbers to have any preceding zeros. Also, while assigning values to the given integer types their range must be kept in mind otherwise the statement will become invalid.
- Octal: Numbers with a base 8 are known as octal numbers. Octal values must have a preceding zero to identify it as octal in Java. Also octal can have values only from 0 to 7. For example:
byte b=04; short s=015; int i=056; long n=0456;
Note that if we write a statement as:
System.out.println(s);
The output will be 13.
The number is converted to its decimal equivalent. - Hexadecimal: Numbers with a base 16 are known as hexadecimal numbers. In Java, they are represented with a preceding zero-x or 0x(or 0X). For example:
byte b=0x4; short s=0x1a; int i=0x5c; long n=0x5a6;
Printing the values of these variables will again result in its decimal equivalent.
The Binary Literal
Java 7 introduced a new kind of literal which could be used to initialize integer type variables, i.e., binary literals. Binary numbers have base 2. These are represented with a 0b or 0B preceding the binary number. For Example:
int a=0b1100;
Negative binary literals can simply be denoted by adding a minus sign(–) at the beginning of the number as given below:
int n = -0b1011;
One great advantage of binary literal is that the addition of binary literals makes it easier to enter values used as bitmasks whose visual meaning was usually lost when decimal or hexadecimal numbers are used. Also, binary literal is very helpful in cases of bit-oriented systems like processors and bit mapped hardware devices. When initializing integer types with binary literals it is important to keep in mind the range of the integer types. For example:
byte b=0b10101011;//invalid statement
The range of the byte type(-128 to 127) is exceeded in this case. Thus it shows an error. Also by default, all the literals are treated as int in Java. So, when dealing with numbers exceeding the 32-bit range it is important to use long by explicitly writing an ‘l’ or ‘L’ as a suffix to the number. For Example:
long n=0b101110111011101110111011101110110L;
This statement is completely valid in Java. But if we miss the L at the end of the number it shall show a compilation error.
Underscores in Java Literals
Another feature that was added in Java7 was the use of underscore. When working with large numbers in any format, we can insert underscores anywhere in between the digits. For example:
long n = 0b1011_1011_1011_1011_1011_1011_1011_10110L;//binary literal long n1 = 1234_5678_9012L;//decimal literal float num = 33.65_2267F; //decimal literal long l = 0X5A_B7_66_EFL; // hex literal
This makes all the literals more readable by separating a group of digits. We can also put more than one underscore between the digits. The presence of underscores only separates the digits. It does not in any way changes the value of the digit. As the above variable n shall yield 6299285366 as output with or without the underscores. It is useful when encoding telephone numbers, ID numbers, etc. The following example shows the use of underscores in literals:
/*This program show the use of literals in different number formats with underscore in between the digits*/ public class UnderscoreDemo { public static void main(String args[]) { long n = 0b1011_1011_1011_1011_1011_1011_1011_10110L;//binary literal long n1 = 1234_5678_9012L;//decimal literal float num = 33.65_2267F; //decimal literal long l = 0X5A_B7_66_EFL; // hex literal int o = 052; //octal literal /*Displaying all the literals*/ System.out.println("Binary literal value : " + n); System.out.println("decimal literal value(no floating point) : " + n1); System.out.println("decimal literal value(floating point) : " + num); System.out.println("Hexadecimal literal value : " + l); System.out.println("Octal literal value : " + o); } }
Output:-
Binary literal value : 6299285366
decimal literal value(no floating point) : 123456789012
decimal literal value(floating point) : 33.652267
Hexadecimal literal value : 1521968879
Octal literal value : 42
Note: The underscore CAN NOT be added at the beginning or end of a number or adjacent to the ‘L‘ or ‘F‘ for long or float or any decimal point. Also, it cannot be added in the case where there is a string of digits.
Hope this tutorial helps you. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.
Do not forget to share and Subscribe.
Keep Learning… Happy Learning.. 🙂