Wednesday, July 9, 2014

Java 7 Features

#1 Strings in switch

#2 try-with-resources statement

#3 More precise rethrow

#4 Multi-catch

#5 Binary integral literals
With Java 7, you can now create numerical literals using binary notation using the prefix “0b”

int n = 0b100000;
System.out.println("n = " + n);


n = 32

#6 Underscores in numeric literals

#7. Improved type inference for generic instance creation
With Java 5 and 6
Map> retVal = new HashMap>();

Note that the full type is specified twice and is therefore redundant. Unfortunately, this was a limitation of Java 5 and 6.

With Java 7
Java 7 tries to get rid of this redundancy by introducing a left to right type inference. You can now rewrite the same statement by using the <> construct.
Map> retVal = new HashMap<>();

#8 More new I/O APIs for the Java platform (NIO.2)
a) Package
The most important package to look for is java.nio.file. 
b) The java.nio.file.Path interface
Old Way
File file = new File("hello.txt");
System.out.println("File exists() == " + file.exists());

New Way
Path path = FileSystems.getDefault().getPath("hello.txt");
System.out.println("Path exists() == " + Files.exists(path));

c) The java.nio.file.Files class
d) WatchService API