Javatpoint Logo
Javatpoint Logo

Equals() and Hashcode() in Java

The equals() and hashcode() are the two important methods provided by the Object class for comparing objects. Since the Object class is the parent class for all Java objects, hence all objects inherit the default implementation of these two methods. In this topic, we will see the detailed description of equals() and hashcode() methods, how they are related to each other, and how we can implement these two methods in Java.

Java equals()

  • The java equals() is a method of lang.Object class, and it is used to compare two objects.
  • To compare two objects that whether they are the same, it compares the values of both the object's attributes.
  • By default, two objects will be the same only if stored in the same memory location.

Syntax:

Parameter:

obj: It takes the reference object as the parameter, with which we need to make the comparison.

Returns:

It returns the true if both the objects are the same, else returns false.

General Contract of equals() method

There are some general principles defined by Java SE that must be followed while implementing the equals() method in Java. The equals() method must be:

  • reflexive: An object x must be equal to itself, which means, for object x, equals(x) should return true.
  • symmetric: for two given objects x and y, x.equals(y) must return true if and only if equals(x) returns true.
  • transitive: for any objects x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • consistent: for any objects x and y, the value of x.equals(y) should change, only if the property in equals() changes.
  • For any object x, the equals(null) must return false.

Java hashcode()

  • A hashcode is an integer value associated with every object in Java, facilitating the hashing in hash tables.
  • To get this hashcode value for an object, we can use the hashcode() method in Java. It is the means hashcode() method that returns the integer hashcode value of the given object.
  • Since this method is defined in the Object class, hence it is inherited by user-defined classes also.
  • The hashcode() method returns the same hash value when called on two objects, which are equal according to the equals() method. And if the objects are unequal, it usually returns different hash values.

Syntax:

Returns:

It returns the hash code value for the given objects.

Contract for hashcode() method in Java

  • If two objects are the same as per the equals(Object) method, then if we call the hashCode() method on each of the two objects, it must provide the same integer result.
Equals() and Hashcode() in Java

Note: As per the Java documentation, both the methods should be overridden to get the complete equality mechanism; using equals() alone is not sufficient. It means, if we override the equals(), we must override the hashcode() method.

Example:

Output:

a & b are equal variables, and their respective hash values are: 1965574029 & 1965574029

c & d are Un-equal variables, and their respective hash values are: 74113750 & 71933245

In the above example, we have taken two 4 variables, out of which two are equal, and two are unequal. First, we have compared the objects whether they are equal or unequal, and based on that, printed their hash values.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA