Binary Literals in Java - Java 7 Feature With Examples

Binary literals are new features in Java 7. As you all know that we can write integral types (byte, short, int, and long) in Binary and Hexadecimal formats. However, from Java 7 onwards we can write these numbers in binary format also. The number should be prefixed with 0b or 0B to be treated as binary literal.

Binary Literals in Java

This feature is very helpful to bit-oriented systems like processors, network protocols and bitmapped hardware device. Earlier the programmers used to transform from binary to decimal/hexadecimal and vice versa. Using this feature will remove this transformation and chances of error will be less in this conversion.

Also, the code using bitwise operations will be more readable with this feature.

Let’s see binary literals in action with a simple java program:

Output of the above program is:

By admin

Leave a Reply

%d bloggers like this: