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
- Reflexive. For any non-null reference value x, x.equals(x) should return true
- 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.
- 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.
- 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.
- For any non-null reference value x, x.equals(null) should return false.
So next time overriding equals() method, do remember these guidelines .
Comments
Post a Comment