The second bit doesn't throw an NPE because you are not dereferencing a null pointer. That code returns false because you are comparing a value to a non-value.
equals(null)
will always return false because there is no such thing as a null value. Neither Object nor primitive can have the value null
since the concept doesn't exist in Java. null
is a literal that represents the null reference which is why we compare references, such as if (obj == null)
. See the Java language spec, section 3.10.7. In other words, you are comparing the value of someObject
to the null reference.
You could make your own object, override equals
, and return true but that would go against the definition in Object.