If null has no value, then foo.equals(null) should raise an exception rather than returning true/false, since by this logic it makes no sense to perform the operation.
Likewise, Objects::equals should not allow null values at all.
That's a clash of concerns - equals is specced to never throw. And removing that now is an issue. But yes, that was a mistake too. It's not the only aspect of java that is a mistake, but fixing it in a backwards compatible way would be a cure that is worse than the disease so it isn't done.
Objects.equals is pointless if 'null should throw' is the mindset. Just call a.equals(b) instead of Objects.equals(a, b) then.
At best a sane equals can be written that does a.equals(b) if both are non-null and throws NPE if either (or both) are, but, 'make a utility method' is a shitty solution to these problems.
View all comments
Show parent comment
1
u/nekokattt Feb 21 '26
If null has no value, then
foo.equals(null)should raise an exception rather than returning true/false, since by this logic it makes no sense to perform the operation.Likewise, Objects::equals should not allow null values at all.