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;

}

113 Upvotes

136 comments sorted by

View all comments

Show parent comment

4

u/kiteboarderni Feb 17 '26

Old man shouts at clouds...this api has been available since 2017. Almost 10 years ago.

0

u/narrow-adventure Feb 17 '26

Gosh darn kids and their modern 10yo APIs lol