Using underscore in Numeric Literals in Java
Last Updated :
02 Mar, 2022
A new feature was introduced by JDK 7 which allows writing numeric literals using the underscore character. Basically, they are broken to enhance readability. This feature enables us to separate groups of digits in numeric literals, which improves the readability of code. For instance, if our code contains numbers with many digits, we can use an underscore character to separate digits in groups of three, similar to how we would use a punctuation mark like a comma, or a space, as a separator. Let us explore further with an example
Example
Java
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int inum = 1_00_00_000;
long lnum = 1_00_00_000;
float fnum = 2 .10_001F;
double dnum = 2 .10_12_001;
System.out.println( "inum:" + inum);
System.out.println( "lnum:" + lnum);
System.out.println( "fnum:" + fnum);
System.out.println( "dnum:" + dnum);
}
}
|
Output
inum:10000000
lnum:10000000
fnum:2.10001
dnum:2.1012001
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...