Clean Code with underscores in literals

As of java 1.7 the java language specification allows integer literals to contain underscores in order to make them more readable. If it is applied in a good way it can enormously increase the readability of your code.

In java 1.6 one had to define integer literals like this:

int iterations = 10000000;
long bytes = 0b11010010011010011001010010010010;

With java 1.7 such integer literals can be written more readably by using the underscore as a  thousands separators.

int iterations = 10_000_000;

or when using binary literals one can use the underscores to group bytes

long bytes = 0b11010010_01101001_10010100_10010010;

 

Further information can be found at


Recommended Reading

 

Leave a Reply

Your email address will not be published. Required fields are marked *