Using underscore to improve readability

suggest change

Since Java 7 it has been possible to use one or more underscores (_) for separating groups of digits in a primitive number literal to improve their readability.

For instance, these two declarations are equivalent:

int i1 = 123456;
int i2 = 123_456;
System.out.println(i1 == i2); // true

This can be applied to all primitive number literals as shown below:

byte color = 1_2_3;
short yearsAnnoDomini= 2_016;
int socialSecurtyNumber = 999_99_9999;
long creditCardNumber = 1234_5678_9012_3456L;
float piFourDecimals =  3.14_15F;
double piTenDecimals =  3.14_15_92_65_35;

This also works using prefixes for binary, octal and hexadecimal bases:

short binary= 0b0_1_0_1;
int octal = 07_7_7_7_7_7_7_7_0;
long hexBytes = 0xFF_EC_DE_5E;

There are a few rules about underscores which forbid their placement in the following places:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents