Sunday, August 28, 2011

Article: Java 7: Worth A Hard Look

I just finished reading a half-page article called "Java 7: Worth A Hard Look" by Andrew Binstock in InformationWeek. If you want to read the article you can download the pdf from InformationWeek. Their site has a broken link to the article.

In the article he briefly mentions a couple of the new items in Java 7 such as NIO.2, fork-join framework, and invokedynamic.

There is no significant meat to the article, but he misses a few of the really cool language improvements: diamond operator, try with resources, and multi-catch. These are changes from project coin which most developers can take advantage of immediately.

The diamond <> operator simplifies the use of generics by reducing code duplication.

Map<String,Map<String,String>> map = new HashMap<>();

Try with resources allows us to use critical system resources like I/O and not have to worry about closing them. The system will handle it for us. All of the Java I/O libraries have been retrofitted with the AutoClosable interface.

The multi-catch allows us to catch multiple exceptions and handle them in a common way.

try {
} catch (NullPointerException | ArithmeticException e) {

This is a lot simpler, and makes the code cleaner and easier to read.

The other items like fork-join, and invokedynamic are much more specific (niche). If you are not using other JVM languages, invokedynamic is not of interest to you. Fork-Join is (in my opinion) more of an academic problem. I have not seen a real good use case example.

NIO.2 is really cool, but it will require a lot more work on the part of the developer to take advantage of. The FileSystem API is awesome for anyone who needs to take advantage of manipulating files on a system. However, it will take some re-work to bring into existing code.


Kumar Pallav said...

That's awesome Man, but lets reduce code lines :D
}catch(Exception e){

John Yeary said...

I am not sure if that was a joke? If it wasn't you missed the point of multi-catch. Anyway, if you are going to use the code snippet you posted, you may want to make it...

try{...}finally {

Popular Posts