r/java • u/edurbs • 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
View all comments
Show parent comment
1
u/narrow-adventure Feb 18 '26
Maybe I’m wrong, I’ve seen people miss understand classes and variables and pass by value semantics over and over again in interviews, so I think they’ll misunderstand and misuse this too. But it’s like my opinion, if it works for you and all devs are using it right maybe I’m just wrong and that’s cool too.