Tuesday, August 2, 2011

In JDK 7, numeral representation get change and it looks awesome

Binary Integral Literals
In JDK7, there is new implementation of integral values and that too in biinary form. This implementation provides user with way of given integral values in binary form and JVM internally convert it into short, byte, int and long.
For e.g.
public class BinaryLiteral {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0b10101;
byte b = 0b1010111;
short s = 0b0101111111111111;
System.out.println("Value of i = " + i + ", Value of short = " + s + ", Value of byte = " + b);
}

}

In above program, there is no compilation error and it prints proper value as this representation of integral value is supported in new JDK.

Underscore in numeric literal
Earlier, user cannot assign numeric literals with separator and it is difficult to deal with long numerals as it cannot be break into small chunks like credit card number (it has 16 digit but if we represent in numeral, it is one chunk of 16 digit), Phone numbers, etc.
In latest JDK, underscore is used as digit separator with numeral integral so that long numerals can represent as {chunk of digit}_{remaining chunk of digit}. And during compilation, JVM parses it correctly.
For e.g.
int i = 123_345_234; //supported in new JDK, won't be possible with earlier versions
This provides better readability of numerals which are long.
For e.g.
public class IntegralTypeTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 167_968;
float f = 67_9.89_0f;
System.out.println("Integer val = " + i + ", Float val = " + f);
System.out.println("Sum of two = " + (i + f));
}

}

This program successfully compiles and prints correct result with new JDK.
There are certain limitations on placing underscore in numeral literals :
  • At the beginning or end of a number, underscore cannot be placed
  • Adjacent to a decimal point in a floating point literal, underscore cannot be placed
  • Prior to an F or L suffix, underscore cannot be placed
  • In positions where a string of digits is expected, underscore cannot be placed
For e.g.
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix

int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)

int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number

int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
int x11 = 052_; // Invalid; cannot put underscores at the end of a number

No comments:

Post a Comment