Tuesday, 19 February 2013

Binary literals and underscores in literals – JDK7


JDK7 is providing many new features. In this post I am going to explain something about binary literal and underscores in literals.

      1. Binary literals:-
JDK7 allows you assign binary numbers to basic data types like byte, short, int and long. These binary literals are useful in microprocessor programming and bitmap representation.  These binary literals makes your data representation more readable that hex or octal number system. Only the thing is that you have start binary literal with “0b” or “0B”.

Here arefew examples of binary literal,
byte binaryByte = (byte)0b001;
short binaryShort = (short)0b001;
int binaryInt = 0b001001;
long binaryLong = 0b0010001L;


      2. Underscores in literals:-
JDK7 allows you to use underscores in number literals. The main intention behind this is making big numbers more readable. 

This feature is running with some rules as given below,
a.       “_” can’t be used after the number.
b.      “_” can’t be used before or after “.” In floating number.
c.       “_” can’t be used before or after “L” or “F”.
d.      “_” can’t be used in between “0” and “b”

Here are few examples,
Int underInt = 0b001_001;
long underLong = 99_99_99L;
long underLong = 0b01_01_01L;

No comments:

Post a Comment