How to use use underscore in numeric literals in java

JDK 7 introduces a new feature that allows the use of underscore characters to write digital text. The digital text is broken to improve readability. This feature allows us to separate the numbers in the digital text, thereby improving the readability of the code. For example, if our code contains numbers with many numbers, we can use the underscore character to divide the numbers into three groups, similar to the way we use punctuation marks (such as commas or spaces) as separators. The following examples show the different ways we can use underscores in numeric text:

 

// Java program to demonstrate that we can use underscore
// in numeric literals
class Test
{
	public static void main (String[] args)
			throws java.lang.Exception
	{
		int inum = 1_00_00_000;
		System.out.println("inum:" + inum);

		long lnum = 1_00_00_000;
		System.out.println("lnum:" + lnum);

		float fnum = 2.10_001F;
		System.out.println("fnum:" + fnum);

		double dnum = 2.10_12_001;
		System.out.println("dnum:" + dnum);
	}
}

Output:

inum: 10000000
lnum: 10000000
fnum: 2.10001
dnum: 2.1012001

 

Submit Your Programming Assignment Details