Example & Tutorial understanding programming in easy ways.

What is binary literals feature in jdk 1.7?

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 before java 7 but 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. That will make the developer to write some more expressive instances that can be identified easily.

Example:-

package r4rjdk7;

public class Java7Literals {

public static void main(String[] args) {

   int i=0b0111;

   byte b=(byte) 0b0111;

   long l=(long) 0B0111L;

   System.out.println("i="+i);

   System.out.println("b="+b);

   System.out.println("l="+l);

 }

}


Read More →