MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1r7iv5p/objectsrequirenonnullelse/o6ezjzc/?context=3
r/java • u/edurbs • Feb 17 '26
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;
}
136 comments sorted by
View all comments
2
My how did I go so long without this feature was the Optional class. So many null pointer checks and exceptions could have been avoided.
View all comments
2
u/strat-run Feb 20 '26
My how did I go so long without this feature was the Optional class. So many null pointer checks and exceptions could have been avoided.