MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1r7iv5p/objectsrequirenonnullelse/o5xsuoc/
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
39
Probably it’s because I’m used to requireNonNull, but this name always suggests an exception to me. firstNonNull(…,…)?
15 u/ThierryOnRead Feb 17 '26 Maybe getIfNull like in apache commons ? Although I liked his previous name defaultIfEmpty 10 u/xome Feb 17 '26 Well, you will get an exception if the second argument is null. 4 u/Jaded-Asparagus-2260 Feb 18 '26 Guava actually named it firstNonNull().
15
Maybe getIfNull like in apache commons ? Although I liked his previous name defaultIfEmpty
10
Well, you will get an exception if the second argument is null.
4
Guava actually named it firstNonNull().
firstNonNull()
View all comments
39
u/Evening_Total7882 Feb 17 '26
Probably it’s because I’m used to requireNonNull, but this name always suggests an exception to me. firstNonNull(…,…)?