In Java, can I define an integer constant in binary format?

Aaron Fi picture Aaron Fi · May 15, 2009 · Viewed 80.2k times · Source

Similar to how you can define an integer constant in hexadecimal or octal, can I do it in binary?

I admit this is a really easy (and stupid) question. My google searches are coming up empty.

Answer

Russ Hayward picture Russ Hayward · Nov 7, 2009

In Java 7:

int i = 0b10101010;

There are no binary literals in older versions of Java (see other answers for alternatives).