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:
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:
Note that if we write a statement as:
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:
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:
Negative binary literals can simply be denoted by adding a <em>minus sign</em>(<strong>-</strong>) at the beginning of the number as given below:
[code language="java"]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:
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:
Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/IntegerLiteral/UnderscoreDemo.java
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.. 🙂