Java 7 enhanced syntax for numeric literals

21 11 2015

Two convenient syntax improvements were implemented in Java 7 in this topic:

  • Numeric constants can now be expressed as binary literals
  • underscores can be used in integers as a separator for sake of readability

Binary literals

Before Java 7 to get an Integer value from binary literal you needed to parse it like that:

int i = Integer.parseInt("10110011", 2);

There is a set of problems with this implementation (it’s verbose, not popular two-argument parseInt(), RuntimeException on typo).

In Java 7 there is a solution – new syntax that fixes all issues and is neat:

int i = 0b10110011;

cute :)

Underscores in integers

In every day life, you prefer to have phone number written like this:


or like this:


This is why since Java 7 you can separate long integers with underscore to improve readability:

int milis = 3_600_000;

The underscore is removed by javac while generating .class files




Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: