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;
}
115
Upvotes
View all comments
Show parent comment
2
u/joemwangi Feb 18 '26
You asked for my reasoning, so I tried to summarise it clearly for a vast topic. If giving a detailed answer reads as arrogance, that’s probably more about tone perception than intent. I was assuming you are a smart guy that didn't require much watering down of terminologies, because I wasn't responding to your "99% of them wouldn't be able to understand what you're saying", which seems to me more likely of an arrogant reply for my taste :). And thanks for assuming I'm young.