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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.journaldev.util; 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); } } |
Output of the above program is:
1 2 3 4 5 6 |
<span style="color: #008000;"><strong><code> i=7 b=7 l=7 x=7 </code></strong></span> |