r/java • • Feb 17 '26

Objects.requireNonNullElse

I must have been living in a cave. I just discovered that this exists.
I can code

City city = Objects.requireNonNullElse(form.getCity(), defaultCity);

... instead of:

City city = form.getCity();

if(city == null){

city = defaultCity;

}

112 Upvotes

136 comments sorted by

View all comments

Show parent comment

3

u/narrow-adventure Feb 18 '26

Totally, I think you nailed it down. I don’t think that an average Java project has enough of those to justify a language change, obviously the committee overseeing Java development disagrees and based on the comments and a lot of Java devs disagree with me too, which is totally cool.