Monday 27 June 2011

Binary integral literals

I can’t say I’ve ever felt the absence of this rather unusual feature. Though it seems it was felt compelling enough to be added in. This is primarily a readability advantage – a semantic representation.

public class BinaryLiterals {

public static void main(String[] args) {

// An 8-bit 'byte' literal.
byte aByte = (byte) 0b00100001;

// A 16-bit 'short' literal.
short aShort = (short) 0b1010000101000101;

// Some 32-bit 'int' literals.
int anInt1 = 0b10100001010001011010000101000101;
int anInt2 = 0b101;
int anInt3 = 0B101; // The B can be upper or lower case.

// A 64-bit 'long' literal. Note the "L" suffix.
long aLong = 0b1010000101000101101000010100010110100001010001011010000101000101L;

}

}

No comments:

Post a Comment