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;
}
113
Upvotes
View all comments
Show parent comment
1
u/narrow-adventure Feb 18 '26
Hmm so for example the function from the comment requireNonNullElse, I just googled it and it looks like it’s been added in 2017 Java 9, did it really need to be, if people wanted it couldn’t they have just included a library?
I feel like after Java 8 the release cadence has sped up drastically leading to a ton of standard library expansions/features being included into the language that should not have been included. There are way too many ways to get things done and I think that over time it will lead to code quality drops (due to devs misunderstanding the code/language features).
I’ll do another hot take: If Java keeps adding features at this pace it will eventually become another scala.
Got the hot take in now we wait for the dislikes haha