blog community
Java 7 and the Elvis operator

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.

Posted 02-03-2009 19:53 by peterhe
Filed under:

Comments

Sander Mak wrote re: Java 7 and the Elvis operator
on 04-03-2009 10:15

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

Wouter van Vugt wrote re: Java 7 and the Elvis operator
on 05-03-2009 15:50

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.

Frappee wrote re: Java 7 and the Elvis operator
on 16-03-2009 22:16

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";

MeBigFatGuy wrote re: Java 7 and the Elvis operator
on 23-03-2009 23:13

how about

String aMember = g??.members[0].name : "nobody";

means inject ?. everywhere there's a dot.

Chris wrote re: Java 7 and the Elvis operator
on 29-08-2009 19:23

@MeBigFatGuy I like that idea!  Groovy is nice with the ?. syntax, but you're right, a lot of times you're just traversing down to the last property/method so why not have a shortcut syntax for that.  

X wrote re: Java 7 and the Elvis operator
on 30-08-2009 0:44

how about

name = (g.members[0].name)? "Anon"

All or nothing operator.

Juliano wrote re: Java 7 and the Elvis operator
on 01-09-2009 3:50

?:| Well, its one for the money,

Two for the show,

Three to get ready,

Now go, cat, go.  

Elias Mårtenson wrote re: Java 7 and the Elvis operator
on 30-12-2009 14:51

The proposal is not only ugly, it's rather useless if not counter-productive.

If you are doing these kind of checks a lot, especially in chains like in the above example, chances are there is something wrong with your design.

Doug Hammond wrote re: Java 7 and the Elvis operator
on 05-01-2010 11:14

I'd also like to see this:

for (Item i ?: list)

Should be self-explanatory.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Enter code (required)
Powered by Community Server (Commercial Edition), by Telligent Systems