is there a Java equivalent to null coalescing operator (??) in C#? [duplicate]

Sadly – no. The closest you can do is:

int y = (x != null) ? x : -1;

Of course, you can wrap this up in library methods if you feel the need to (it’s unlikely to cut down on length much), but at the syntax level there isn’t anything more succinct available.

Leave a Comment