Using underscore to improve readability
suggest changeSince 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:
- At the beginning or end of a number (e.g.
_123
or123_
are not valid) - Adjacent to a decimal point in a floating point literal (e.g.
1._23
or1_.23
are not valid) - Prior to an F or L suffix (e.g.
1.23_F
or9999999_L
are not valid) - In positions where a string of digits is expected (e.g.
0_xFFFF
is not valid)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents