Characteristics of Object.equals()

I am come across the characteristics specified for implementation of equals() method in Java Classes. There is nothing like which we don’t know, but here it is well explained. I thought of sharing the same just for recalling old Java learning days

Here are the characteristics that every implementation of Object.equals() should follow

  1. Reflexive. For any non-null reference value x, x.equals(x) should return true
  2. Symmetric. For any non-null reference values x and y. x.equals(y) should return true if and only if y.equals(x)  returns true.
  3. Transitive. For any non-null references value x,y and z , it x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  4. Consistent. For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  5. For any non-null reference value x, x.equals(null) should return false.

So next time overriding equals() method, do remember these guidelines .

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester