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 thoughts on “Java 7 and the Elvis operator

  1. 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.

  2. 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”;

  3. Pingback: Belgisches Bier + Java = Devoxx – Dag 1 – about:Namics

Comments are closed.