With Java 6 out for quite a while now, Java 7 is finally looming closer for a release due in early 2010. Most of the big changes are either scrapped (like support for closures or a standard module system), or actually pushed into a Java 6 update (the new G1 garbage collector is coming in update 14).
Not all is lost on Java 7 though. Along with several API updates and JVM/tooling improvements, there are also some language changes expected. These are gathered in "Project Coin". The clever name implies that each proposal involves "small" change, and refers to the proverb "to coin a phrase".
One of the proposals is the new "Elvis" operator. It involves using the question mark as a shorthand to check for null scenarios. One of the expressions is this one:
?:
If you look sideways, it's Elvis! I really am not that fond about this kind of programming, but having something called the Elvis operator as a new feature is reason enough for me to migrate asap. 😉 I hope other language additions will get a similarly cool name.
4 comments
Well, at the very least it is a start for fixing the ‘Billion dollar mistake’ called null pointers… http://developers.slashdot.org/article.pl?sid=09/03/03/1459209&from=rss
Sander Mak
A sample from the document you link to:
final String aMember = g?.members?[0]?.name ?: “nobody”;
That’s a lot of question marks! IMHO totally unreadable.
Wouter van Vugt
consider the alternative…
Person[] t1= g != null ? g.members : null;
Person t2 = t1 != null ? t1[0] : null;
Name t3 = t2 != null ? t2.name : null;
final String aMember = t3 != null ? t3 : “nobody”;
Frappee
[…] etc. Als Beispiel wurde im Vortrag der Sprache Java ein neuer Operator, Elvis Operator, “begebracht”. Ob wir das hier je brauchen, weiss ich nicht. Einen Einblick zu […]
Belgisches Bier + Java = Devoxx – Dag 1 – about:Namics