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;
}
111
Upvotes
View all comments
Show parent comment
1
u/narrow-adventure Feb 17 '26
Idk, maybe y’all are working on projects where that makes sense. I can’t imagine what those would be but if it works for you and you think it’s a 1000x better - more power to you!