JDK 7.0 is would be released to the general public some time soon. Developers can try milestone version now. The 4 major changes in the JDK 7 from a programming perspective are:
- Ability to use underscores in numerals.
- Ability to use Diamond syntax with collections + generics.
- Ability to use Strings for comparison in switch statements.
- Ability to use binary literals
Let’s review of these changes in a bit detail.
Underscores in Numerals
With this feature you can use underscore in numerals to make them more readable.
int i = 1_5;
i = i*10;
This seems to help with the readability of the numbers. The JVM would strip all of these underscores for doing all math operations.
1_5 * 10 would be 15 * 10 = 150
In cases where parametrized types need to be explicitly declared for a constructor, and the full parametrized type < T1 , T2 , …Tn > of that constructor is obvious from the context, then the parametrized type of the constructor can be replaced with an empty set of type parameters: <>. The <> construct is legal to use when constructing an object and either assigning it to a variable, or when passing it as a parameter.
For example, consider the following assignment statement:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
This is rather lengthy, so it can be replaced with this:
Map<String, List<String>> anagrams = new HashMap<>();
String in Switch Statement
Now you can compare String in the case statement and don’t use int data type only like before. However should keep in mind that String comparison are slower than the int comparison but for sure is handy feature.
String s = “bar”;
Use of Binary Literals
With this feature one can define binary numbers as literals in the code by prefixing it with 0b. Then these can be converted into other data types like byte , short etc. with simple casting.
byte b = (byte)0b0001;
short s = (short)0b0010;
System.out.println(“Byte is : ” + b + ” Short is : ” + s);
Out of the these 4 changes, the most useful is the String usage in switch statement followed by <> syntax.
The other changes that did not make JDK7 were:
- Improved Exception Handling for Java
- Elvis and Other Null-Safe Operators
- Large arrays