r/java • • 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;

}

113 Upvotes

136 comments sorted by

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.

1

u/narrow-adventure Feb 18 '26

I was just trying to give you a little jab and be funny, this is the comment that made me think you're a bit arrogant:

`Probably he doesn't understand patterns, exhaustiveness, deconstruction etc. A highly probable reason.`

It was just light banter, I actually think I'd really like drinking a beer with you!