- What to Expect for JDK 7
- Binary Literals
- Switch on String
- Underscores in Integer Literals
- Conclusion
Underscores in Integer Literals
JDK 7 supports Derek Foster's Underscores in Numbers (Version 2) Project Coin proposal to improve the readability of binary, decimal, hexadecimal, and octal literals by allowing underscore characters:
int mb_directory_info = 204_555_1212; System.out.printf ("%d%n", mb_directory_info); // Output: 2045551212 long debt = 11_000_000_000_000L; System.out.printf ("%d%n", debt); // Output: 11000000000000 byte max_pos_value = 0x0___07F; System.out.printf ("%d%n", max_pos_value); // Output: 127
Essentially, you can place one or more underscores between successive digits. However, you cannot specify a leading underscore (as in _25) because this would be interpreted as an identifier. Also, an underscore cannot be specified as a suffix (as in 0x3F_).
Although Foster mentions that this feature should be supported by Integer's and Long's decode() methods, they don't support this feature in the current build. Also, Integer.parseInt() and Long.parseLong() don't support this feature.