MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/1r7iv5p/objectsrequirenonnullelse/o5z1v1d/
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
0
[deleted] — view deleted comment
8 u/persicsb Feb 18 '26 There is a method for that: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Objects.html#requireNonNullElseGet(T,java.util.function.Supplier) 0 u/Absolute_Enema Feb 18 '26 What an abomination. 2 u/ryan_the_leach Feb 18 '26 Which shows why operators for this are appreciated so much by the c# community.
8
There is a method for that: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Objects.html#requireNonNullElseGet(T,java.util.function.Supplier)
0 u/Absolute_Enema Feb 18 '26 What an abomination.
What an abomination.
2
Which shows why operators for this are appreciated so much by the c# community.
View all comments
0
u/[deleted] Feb 18 '26
[deleted] — view deleted comment