...
- It is reflexive: For any reference value x,
x.equals(x)
must return true. - It is symmetric: For any reference values x and y,
x.equals(y)
must return true if and only if y.equals(x) returns true. - It is transitive: For any reference values x, y, and z, if
x.equals(y)
returns true andy.equals(z)
returns true, thenx.equals(z)
must return true. - It is consistent: For any 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 object is modified. - For any non-null reference value x,
x.equals(null)
must return false.
Wiki Markup |
---|
Do not violate any of these five conditions while overriding the {{equals}} method. Mistakes due to the first postulate are quite infrequent; it is thus omitted from this discussion. The second and third conditions are highlighted. The rule for consistency implies that mutable objects may not satisfy the {{equals}} contract. It is good practice to avoid defining {{equals()}} implementations so as to use unreliable sources such as IP addresses \[[Bloch 08|AA. Java References#Bloch 08]\] and caches. |
Noncompliant Code Example
...