Sunday, November 28, 2010

Java 7, where are you?

That's right. On the December 11 it will be round 4 years since the first release of Java 6. And I have to admit that it's quiet a long time in comparison to the previous versions. Ok, why don't have a little history lesson right now? Let's go back in time to end of 90s. Right before Christmas period, in '98, on December 8 Java 1.2 is published. A year and a half (May 8, 2000) after this Java 1.3 kicks in. Then again, not even 2 years and we had the Java 1.4 (February 6, 2002). And it keeps going. September 30, 2004 – Java 1.5, December 11, 2006 – 1.6. And today we have November 28 2010 and there is still no Java 7! Nearly 4 years! Of course we had plenty updates to Java 6 (more precisely 22) but I still claim it's a long time. You may probably wonder that there are lots of new features and important changes coming up soon. The reality though, is a little bit different. Soon – yes, in the mid of 2011. Lots and important? Let's have a look. As for the Java 7 we got 2 major Java Specification Requests. JSR #334 Small Enhancements to the Java Programming Language and JSR #335 Lambda Expressions for the Java Programming Language. In a nutshell, the #334 introduces the following features:
  • Strings in switch
  • Binary integral literals and underscores in numeric literals
  • Multi-catch and more precise rethrow
  • Improved Type Inference for Generic Instance Creation (diamond)
  • try-with-resources statement
  • Simplified Varargs Method Invocation
I don't say these changes are not cool. They actually are. For instance the Diamond syntax will let you replace
 Map<String, ArrayList<Integer>> m = new LinkedHashMap<String, ArrayList<Integer>>();  
with this:
 Map<String, ArrayList<Integer>> m = new LinkedHashMap<>();  

Another interesting feature is “try-with-resources statement”. You may wonder what this is. Have a look at the following example (sorry for the lack of proper code formatting but for some reason it doesn't work with this code):
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();

} finally {
With the automatic resource management statement it will look like this:
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
return br.readLine();
This new enhanced try clause will automatically close the resource after the try block so in this case it will never happen that the programmer forgets about it. Oh, and any sorts of streams are welcome in the new try plus you can have more than one.

And my most favourite – the second point. You will be able to use underscores in numeric literals, like this:
int howCoolIsThis = 1_000_000;
No kidding. That's one of the points proposed for the Java 7 specification.

Ok. Let's go to the big one. Lambda expressions! Present in the huge rival of Java – C# and also in other languages like PHP or Javascript. And now very likely to be introduced to Java 7. Here is the glimpse of the request:
  • Lambda Expressions
  • SAM Conversion
  • Method References
  • Virtual Extension Methods
As for me, this is a real boost of Java 7 and the feature that will make an actual difference. Because let's be honest, the changes from the JSR #334 are really minor. Of course, they all are very useful (maybe except the underscore thing...), especially the Diamond syntax and will lead to the better quality of code but aren't these too few changes for 4 years?