How Numeric literal with underscore works in java and why it was added as part of jdk 1.7

Dark Knight picture Dark Knight · Nov 6, 2013 · Viewed 20.8k times · Source

Can somebody please explain to me why this feature was added in JDK 7 and how it works?

While going through the new features of JDK 7, I found following code.

int i;
//Java 7 allows underscore in integer
i=3455_11_11;

Answer

Pradeep Simha picture Pradeep Simha · Nov 6, 2013

This is used to group the digits in your numeric (say for example for credit card etc)

From Oracle Website:

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

To conclude, it's just for a sake of readability.