Method equals() in Java

Why equals() is needed

Equals() method - defines an object equivalence relation.

How is it different from the == operation

When comparing objects with ==, the comparison only occurs between references. When comparing by equals() overridden by the developer, by the internal state of objects.

If you want to override equals(), what conditions should be met? What are the properties of an equivalence relation generated by equals()?

  • Reflexivity: for any reference to a value x, x.equals(x) will return true;
  • Symmetry: for any references to x and y values, x.equals(y) must return true if and only if y.equals(x) returns true.
  • Transitivity: for any references to x, y, and z values, if x.equals(y) and y.equals(z) return true, then x.equals(z) will return true;
  • Consistency: for any references to x and y values, if x.equals(y) is called multiple times, it will always return true, or it will always return false, provided that no information used in comparing the objects has changed.

For any non-null reference to the value of x, the expression x.equals(null) must return false.

Rules for overriding the Object.equals() method.

  1. Using the == operator to test if the argument is a reference to the specified object. If so, true is returned. If the object being compared is == null, then false should be returned.
  2. Using the instanceof operator and calling the getClass() method to check if the argument is of the correct type. If not, false is returned.
  3. Casting the argument to the correct type. Since this operation follows the instanceof test, it is guaranteed to succeed.
  4. Traversing all significant fields in a class and verifying that the value of the field in the current object and the value of the same field in the argument being checked for equivalence match. If the checks for all fields were successful, the result is true; otherwise, false.

When you have finished overriding the equals() method, you should check: is the generated equivalence relation reflexive, symmetric, transitive, and consistent? If the answer is no, the method should be edited accordingly.

Relationship between hashCode() and equals(). If equals() is overridden, are there any other methods that should be overridden?

Equal objects must return the same hash codes. When overriding equals(), be sure to override the hashCode() method.

What happens if you override equals() without overriding hashCode()? What problems can arise?

Classes and methods that use the rules of this contract may not work correctly. So for a HashMap, this can lead to the fact that the key-value pair that was placed in it when using a new instance of the key will not be found in it.

How are the hashCode() and equals() methods implemented in the Object class?

Implementation of the Object.equals() method boils down to checking for equality of two references:

public boolean equals(Object obj) {
  return (this == obj);
}

The Object.hashCode() method implementation is described as native, i.e. not defined using Java code and usually returns the address of an object in memory:

public native int hashCode();


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed