Archive for July, 2011

New in Java 7 (01)

Sunday, July 31st, 2011

Java 7 has finally been released! There have been a lot of discussions around the features that would/wouldn’t be included in Java 7. After a while, I decided to stop checking the news around that subject since it changed so often. And apparently, some of the most awaited features have been deferred to Java 8 or later. Among which: JSR 294 (Language and VM support for modular programming), closures,  or JDK modularization (project Jigsaw). Too bad! But anyhow, now Java 7 is here and it’s time for me to check it out. In this series of posts, I’ll write about the new features that I find interesting as I discover them (don’t except the list to be exhaustive though).

So let’s get started! Here’s the first bit I’ve just read about:

You can now replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (<>: informally called the diamond) as long as the compiler can infer the type arguments from the context.

If we translate this to actual code, it really means that instead of:

Map<Foo,Bar> fooBar = new HashMap<Foo,Bar>();

You can now write this:

Map<Foo,Bar> fooBar = new HashMap<>();

Or this:

Map<Foo,List<Bar>> fooBars = new HashMap<>();

Instead of that:

Map<Foo, List<Bar>> fooBars = new HashMap<Foo, List<Bar>>();

This is a nice addition, but make sure to check out the reference documentation for more details, it doesn’t stop there ;-)