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;
}
111
Upvotes
View all comments
Show parent comment
1
u/rzwitserloot Feb 21 '26
You're likely doing it wrong.
nullshould not equalnull. Objects.equals is bad. SQL gets this better.nullmostly means what you want it to mean, but java itself inherently 'forces' it upon you when you create an array of a reference type (all cells of the array start out asnull; inherent in the language, you can work around it withArrays.fill), and any field that lacks an initializer (or is somehow accessed before that initializer has executed) isnull.But, mostly - it's what you want it to mean.
But here's thing -
nullis inflexible. It throws an exception no matter what question you ask a null object and you cannot change this.Hence, there is one, and only one, meaning that null should ever have: The semantic meaning of "it doesn't exist at all - therefore it is silly to even begin to ask the question". If I ask a string how long it is,
""would respond with 0.nullshould not (and indeed, does not - it 'responds' with an NPE instead). That's correct. Because how long is an unknown string?The answer is.. unknown. That's sort of the point. NPE is what you want here.
I have 2 strings. For whatever reason you cannot access them currently. For example because whatever service was meant to give them to me hasn't given them to me yet.
Are they equal?
Yes. No. Maybe? I don't know!
Objects.equalssays that these 2 unknown concepts are equal. This is, at a fundamental level, wrong. Subtly so perhaps, but wrong.What's really happening is that you're associating semantic meaning to
nullitself. This is a bad idea.The same thing occurs anytime you see this code:
That code is usually a code smell. Because the code was never intended as an actual
ORconstruct. Instead it is semantically confused about howxreferring to some specific semantically well defined state (namely: Empty) is actually represented. Empty string? null? I don't remember, fuck it, let's just cover both bases.This is a bad way to code. And it leads to unexpected NPEs that you then don't like and makes you say delirious things like 'null is a billion dollar invention' or whatever that was.
Objects.requireNonNullElseis not inherently 'wrong' here. That's just a way to convey the idea of: "I want to do an operation on an expression, where that expression is the notion: "If X is initialized, I want X, but if it is not, I want this default value" which is totally reasonable.Objects.equalsis not so lucky. It is semantically confused. "If X and Y are both present, I want to compare them. If one is present but the other is as yet uninitialized, I want to for some bizarre reason conclude that therefore X and Y must not be equal. Except if both are uninitialized; then they ARE equal." This entire concept is hopelessly confused about the difference between a reference itself and the object a reference is referencing.The fact that you think
Objects.equalsis highly useful is why you hate null. Because you're using it wrong.When you do it 'right', null rarely occurs. The classic "If your method returns a list and you want to return the notion that no results have been found, then return an empty list. Do not return
null" is one rather trite example of how to 'do it right'.