Java 7 – The Language Grows Up

Java was side stepped by C# some years ago – but with the newly invigorated roadmap, Java is back in the game!

Java 6 had a few very (very) annoying features which have been fixed in Java 7. It is easy not to realise how powerful these new yet small aspects of the language are. They point to a bright future where Java grows up slowly into a ever easier language to use.

My first insight into the annoying features in Java did not come form writing Java but from creating a COBOL compiler which could compile to the JVM. Some features were asked for by fellow developers which I would not have thought about otherwise:

  1. Some way to make variable definitions easier in the face of generics syntax.
  2. Something like the using syntax in C#.
  3. Lambdas and delegates (these are coming to Java 8).

In this post I am going to take a quick look at 1 and 2. These two features make my every day life much easier as a Java programmer, so I am happy to talk about them!

Diamond Syntax:

Even with excellent tools like Eclipse, writing the following is a pain:

ArrayList<string> list = new ArrayList<string>();

But it gets worse, how about:

ArrayList<ArrayList<string>> list = new ArrayList<ArrayList<string>>();

The compiler know that the type on the right is the same as the type on the left; that is how it does the type reconciliation. So, why not allow programmers to let the compiler do a bit of work for them:

ArrayList<ArrayList<string>> list = new ArrayList<>();

This exactly what the diamond operator does. In simple terms <> means “Take the generic type of the l-value to which the r-value is being assigned.

It is easy to say this sort of change does not matter. Using auto-complete on an IDE developers do not need to do all that typing and what does a few characters matter anyhow? The difference comes in readability as much as in the rate of typing. Code with frequent redefinitions of the same type requires more effort to read and is easier to misunderstand. Misunderstandings cause bugs, need I say more?

Try With Resources:

On the subject of bugs, here is a construct which can cause rather a lot of them:

static String readFirstLineFromFile(String path) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}

Three things tend to happen here:

  1. Someone forgets to put in the finally clause and the system runs out of file handles.
  2. Someone forgets to put in the null check and null pointer exceptions get thrown when the FileReader fails.
  3. The code gets edited for some change and the above two things happen in the new version.

This is another case of the Java language being complete and competent but prone to bugs by relying on the developer a little too much. Java 1.7 brings in the new Try-With-Resources syntax. It looks a little odd to start with but it is straight forward and works really well.

static String readFirstLineFromFile(String path) throws IOException  {
try (
BufferedReader br = new BufferedReader(new FileReader(path));
) {
return br.readLine();
}
}

Now, what ever objects are defined in the () clause after the try will be closed automatically. This is a much better approach in that, just like the the diamond operator, we only need to think about the issue once, in one place. For more information on try-with-resources see the Oracle Page.

By: Dr Alexander J Turner: I would like to thank Attila for contacting me and organising this interesting project. Feel free to pop over to my blog at Nerds-Central at any time 🙂

Meta: this post is part of the Java Advent Calendar and is licensed under the Creative Commons 3.0 Attribution license. If you like it, please spread the word by sharing, tweeting, FB, G+ and so on! Want to write for the blog? We are looking for contributors to fill all 24 slot and would love to have your contribution! Contact Attila Balazs to contribute!