Objects.equals(null, null); should return false

2 weeks ago 12
ARTICLE AD BOX

I think that Objects.equals method is wrongly implemented. it shold return false too if both objects are null.

With this logic this is true, but should not: Objects.equals((String) null, (Integer) null)

It has a practical reason to be false. for example if I have following Entity:

class Person { Long id; public boolean equals(Person other) { ... return Objects.equals(this.id, other.id); } public int hashCode() { ... } }

in this case it would be nice if not persisted persons (where person.id == null) would not be equal. It would be nice to store not persisted persons for example in a

Set<Person> persons = Set.of(new Person(), new Person());

now is

persons.size() == 1; // this should not be

is my assumption correct?

Read Entire Article