Java The Language Vs. Java the Platform

New and Noteworthy in JDK7

The recent preview release of JDK7 is the first public message Oracle has sent amidst the uncertainty about the future of Java. JDK7 was originally targeted for 2008-2009 and promised some great new language features, most notably lambda support, new collections support and unsigned literals. Some twenty-four months late, the preview release includes only a handful of new language features but given the rocky road so far, that's probably to be expected. In this article, we'll take a closer look at some of the new features and discuss how useful they're actually likely to be and by extension, Java's place in the overcrowded language market.

Java has started to show its age, it's over sixteen years old now and hasn't kept up with the modern developer. The JVM has proven itself as a serious platform for execution but the language itself has started to feel dated. With the current trend towards functional-style programming and the rise of JVM targeted languages such as Scala, Java the language has found itself in the position where it has to compete with Java the platform. Can the new language features of JDK7 bolster the language's position or is it just too late? A summary of the new and noteworthy language features include:

• Type inference on generic object creation

• Try-with-resources statements

• Catching multiple exceptions in a single catch block

 

Type Inference on Generic Object Creation and Constructor Arguments

This new feature allows a little brevity to the garrulity of the language, at least when instantiating new generic objects when the type can be inferred. For example,

 

private Map<Size, List<Shoe>> stock = new HashMap<Size, List<Shoe>>();

 

can be reduced to:

 

private Map<Size, List<Shoe>> stock = new HashMap<>();

 

where the diamond operator can be inferred from the declaration. It’s subtly different than leaving out the generic completely, which would reduce your type to being of Object. General inference rules apply. So for example, return types of methods can be used to infer the type as in the example below.

 

 

 

Things don’t get much better than this. Actually, they do. Just a little. Constructor generics always used to be fun and that hasn’t really changed, although with JDK7 you can do a little more. For example,

 

 

These examples are the same as the ones Oracle give (more or less), they both work with JDK7 only and show the Integer type inferred as the class generic (X) in combination with the diamond operator. The second example shows new syntax to explicitly set type of the method generic to give some additional compile time checks. Specifically, if you attempt something crazy such as:

 

 

you’ll see the friendly compilation error like this

 

 

Interestingly, Oracle’s own examples don’t actually compile against the preview JDK7 release. In the official documentation, they show the following

 

 

which wouldn’t compile for me, citing a cannot infer type arguments for MyClass<> error. This would imply that you can’t use the diamond operator with explicit type specification against generic constructor arguments. This just sounds too flakey, I’m sure it’s an oversight and subsequent updates will move inline with Oracle’s documentation. They have also seemingly slipped a typo into the official documentation with a roguè in their example;

 

MyClass<Integer> myObject = new <String`> MyClass<>("")

 

Remove it and things will compile. Leave it and languish. The trouble is, pretty much all the examples on the web have been based on their documentation so cut-and-pasters beware. As an attempt to reduce the visual clutter we’re exposed to, this feature isn’t very impressive at all. In fact, IDEs such as IntelliJ IDEA have been doing it for some time. If you look at the first example above in IDEA, it will automatically use code folding to hide the repetition and display something like the following.

 

private Map<Size, List<Shoe>> stock = new HashMap<~>();

 

combined with the hit and miss documentation, this new language feature from Oracle is decidedly underwhelming.

 

   

Pages

Toby Weston
Toby Weston

What do you think?

JAX Magazine - 2014 - 03 Exclucively for iPad users JAX Magazine on Android

Comments

Latest opinions