Skip to content
Advertisement

Does a mutable Java object equal itself? [closed]

I was thinking about mutable objects and how they’re weird (but very cool).

Question: can a mutable object not equal itself?

Only caveat here is that obviously you must override equals method, otherwise the default checks pointer equality which will (obviously) always be satisfied.

Edit to Question

Alright, I’ve thoroughly confused everyone, take a look at this program:

JavaScript

Advertisement

Answer

The documentation of Object::equals clearly states that:

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.

While we can implement equals(...) in a way that violates this contract (as was mentioned by khelwood in this comment), it will have consequences. Collection CopyOnWriteArraySet, for example, will not function properly:

JavaScript

Ideone demo

(This is a variant of the code I used in my answer to this question by betaRide)


The collection CopyOnWriteArraySet will behave as expected when we remove the (faulty) implementation of equals(...) in Foo and use the default equals(...)-implementation from Object instead:

JavaScript

Ideone demo

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement