Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: grammar fixes in the general description to improve clarity

An object is characterized both by its identity (location in memory) and by its state (actual data). While the The == operator compares only the identities of two objects (to check if both whether the references refer to the same object), ; the equals method defined in java.lang.Object can be customized by overriding to compare the state as well, when customized by overriding. If When a class defines an equals() method, the provider has accounted for state comparison. If not, When the class lacks a customized equals() method (either locally declared, or inherited from a parent class), it uses the default Object.equals() implementation that is inherited from Object which compares only the references and may produce conterintuitive results. For example, the classes String and StringBuffer should override the Object.equals() method because they do not provide their own implementation.

The equals() method only applies to objects, not primitives. Also, immutable objects do not need to override it. There is no need to override equals if when checking logical equality is not useful. For example, Enumerated types have a fixed set of distinct values that may be compared using ==}} instead of {{the equals() method. Note that Enumerated types do have provide an equals() implementation that uses == internally and ; this default cannot be overridden. More generally, if a subclass inherits subclasses that both inherit an implementation of equals() from a superclass and does not need additional functionality, overriding also lack a requirement for additional functionality need not override the equals() can be forgone method.

Wiki Markup
The general usage contract for {{equals()}} as specified by the Java Language Specification \[[JLS 2005|AA. Bibliography#JLS 05]\] says

  • 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 and y.equals(z) returns true, then x.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.

Do not Never violate any of these five conditions while when overriding the equals() method. Mistakes resulting from a violation of the first condition are infrequent; it is consequently omitted from this discussion. The second and third conditions are highlighted below. The rule for consistency implies that mutable objects may not satisfy the equals() contract. It Consequently, it is good practice to avoid defining equals() implementations that use unreliable data sources such as IP addresses and caches. The most common violation of the final condition about the regarding comparison with null is typically violated when the equals() methods whose code throws an exception instead of rather than returning false. When this constitutes a security vulnerability (in the form of denial of service), it can be trivially fixed by returning falsethe simple fix is to return false rather than to throw the exception.

Noncompliant Code Example

...