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
8
u/kubelke Feb 17 '26
What until you find out that you can use Objects.equals that supports null
63
u/zattebij Feb 17 '26
final City city = Optional.ofNullable(form.getCity()).orElse(defaultCity);... is still more readable imo, plus you can use
orElseGetto avoid getting the defaultCity when it's not required.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(…,…)?
8
u/agentoutlier Feb 17 '26
Bonus in that the JIT basically makes it equivalent performance wise.
The only annoying thing about the method is that some null analysis tools do not like when you pass a nullable.. namely checker (you can change the stubs though).whoops I mean the non default fallback one.6
u/nicolaiparlog Feb 18 '26
The
Objects.requireNonNull...methods are most commonly used in constructors to ensure that nonullslips into a field. These fields are most often final and the one-liner works really well with that:If a code base uses these methods with some frequency (mine do), I recommend to statically import them.
13
u/yk313 Feb 17 '26
I’ll give you my ternary operator when you pry it from my cold, dead hands.
City city = from.getCity() != null ? from.getCity() : defaultCity;
7
u/nlisker Feb 17 '26
SO has a long discussion on this. But yes, it's a known API and I'm surprised when people don't know it.
1
u/BeyondFun4604 Feb 18 '26
Which java version were you using
14
u/narrow-adventure Feb 17 '26 edited Feb 18 '26
I personally think that Java is getting worse not better with each of these additions.
If != null is perfectly readable and clear :/ I find myself liking Go more and more each time I see these simplifications that are overly verbose for no reason… but maybe I’m just getting old…
Edit: Thank you everyone for commenting, I've enjoyed reading different perspectives and I really tried to clarify my thoughts and reply to everyone.
4
u/hiasmee Feb 17 '26
Suggestion: Why not do it where form is created?
2
u/strat-run Feb 20 '26
My how did I go so long without this feature was the Optional class. So many null pointer checks and exceptions could have been avoided.
2
u/witness_smile Feb 22 '26
You can even use Optionals and write
And you can even chain it, say the city is in a nested object like Form -> Address -> City
And when any part of the Optional chain is null, it will short circuit to the default value
3
u/brunocborges Feb 18 '26
A-há! It was already there! :D
https://javaevolved.github.io/errors/require-nonnull-else.html
1
u/BordicChernomyrdin Feb 18 '26
Same here! I just used this for the first time last week!
1
u/nekokattt Feb 18 '26
i like requireNonNullElseGet
1
u/red_dit_nou Feb 20 '26
Two words: Ternary Operator
0
u/White_C4 Feb 18 '26
I don't really understand the need to make it into one line. You're adding more unneeded abstraction. If you decide later you need to add more checks if city is null, well you have to go back to the traditional way.
Readability wise, the bottom one is better. Performance wise, the bottom one is still faster (I know for this case the difference is negligible but point still stands). Code adaptability wise, the bottom is more flexible.
-4
u/Gnerglor Feb 18 '26
Or you could just upgrade to Kotlin.
0
u/[deleted] Feb 18 '26
[deleted] — view deleted comment
1
u/[deleted] Feb 18 '26
[removed] — view removed comment