thougts about java7
Today I took some time to take a deeper look at [tag]java7[/tag]. in contrast to java6 the update to version 7 contains some language improvements again. In this post I take a quick look at some of these new language features.
1. Safe NULL-handling
I’m really glad to hear that for example the safe nullpointer dereferencing we already know from [tag]groovy[/tag] will be part of the java language. The syntax seems to be the same as in groovy. the safe dereferencing operator “?” can be used to avoid Nullpointer Exceptions in your code:
1 2 3 | public String getPostcode(Person person) { return person?.getAddress()?.getPostcode(); } |
Instead of causing a NPE if person is a null object, the return value of this method will be null.
Another feature about safe handling with null objects is the null-default operator. In Java7 we happily needn’t write code like the following anymore:
1 2 | String str = getSomeStringProperty(); str = (str != null ? str : ""); |
Writing str = str ?: “”; will do the trick.
These two improvements seemed to be heavily inspired by the groovy dynamic language. But it’s a pity that the groovy truth (null==false) apparently isn’t part of of java7, although the null-default handling is a part of that.
2. Improved catch clause
In Java7 it will be easier to handle two types of catched Exception by introducing the following syntax:
1 2 3 4 5 | try { return new ExampleClass(); } catch (InstantiationException | IllegalAccessException e) { throw new AssertionError(e); } |
You can concatenate two or more exceptions, which you want to be handled the same way, by the using the new “|” operator. I think this is real code sugar. It will safe much lines of code. Till today i wrote private methods if I have to handle the same complex exception handling on different catched exceptions. Now this isn’t necessary anymore.
3. switch statement with a string
I guess many developers wonder why this feature is not yet part of the Java language. I don’t often use switch statements. But if I use it, I really often miss that feature. Since Java7 the switch statements with string values (just like we already use in groovy) will be part of java.
4. Chained Invocation
A really nice new feature will be the support for chained invocation. what does this mean? think about the following code:
1 2 3 4 | Person person = new Person() person.setFirstname("Peter"); person.setSurname("Fox"); person.setAge(42); |
This code isn’t really handsome. you have much boilerplate code for just creating a person. this maybe can be solved using constructers with lots paramters. This isn’t really nice stil. To avoid this boilerplate code the developer can profit on the new chained invocation feature. java7 allows void methods to implicitly return this. this results in the following java7 code:
1 2 3 4 | Person person = new Person() setFirstname("Peter") setSurname("Fox") setAge(42); |
Btw, of course it is already possible to write this code in java5 and earlier, but this requires a breach with the java bean model since you have return this explicitly.
This was just an abstract of the complex java7 topic. I’m really looking forward to the java7 release. Since coding a lot of groovy code the last days, I am a spoiled child who doesn’t want to miss these features in his daily java development.
Finally excuse this bunch of language mistakes. Writing blog entries in english seems to be a good excercise for me and I determined, that english entries achieve more readers than german written ones.



January 30th, 2009 at 10:59
Nice post! Glad to see these features. I really like all of them. Null-handling is a pita, somewhat
the | on exceptions does save some time. And, of course who does not like fluent APIs (->Chained Invocation)?