How to Compare Two Objects in Java
Java Object class is the super class of all the Java classes. All Java classes implements the Object class by default. The Java Object class provides the two important methods to compare two objects in Java, i.e. equals() and hashCode() method. In this section, we will learn how equals() and hashCode() method works. Along with this, we will also learn how to compare two objects in Java with proper examples.
Java provides the two methods of the Object class to compare the objects are as follows:
Java equals() Method
The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.
The method parses a reference object as a parameter. It returns true if the objects are equal, else returns false.
It is also possible that an object is equal to another given object, then the equals() method follow the equivalence relation to compare the objects.
In short, for any non-null reference say x and y, it returns true if and only if both references refer to the same object.
Remember: When we override the equals() method, it is necessary to override the hashCode() method. Overriding follow the convention for the hashCode() method that states, the equal object must have equal hash code.
Example of equals() method
In the following example, we have created constructor of the Double and Long class and passes corresponding values, as an argument that stored in their objects, respectively.
After that, in the first println statement, we have invoked equals() method and parse an object y as a parameter that compares the object x and y. It returns false because x holds the double value and y holds the long value that is not equal.
Similarly, in the second println statement, we have invoked equals() method and parse the same value as in the constructor of the Double class. It returns true because the object of double class i.e. x holds the same value as we have passed in the equals() method.
Objects are not equal, hence it returns false Objects are equal, hence it returns true
Difference Between == Operator and equals() Method
In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects.
Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.
For example, the expression obj1==obj2 tests the identity, not equality. While the expression obj1.equals(obj2) compares equality.
Java hashCode() Method
In Java, hash code is a 32-bit signed integer value. It is a unique id provided by JVM to Java object. Each Java object is associated with the hash code. The hash code is managed by a hash-based data structure, such as HashTable, HashSet, etc.
Remember: When we override the equals() method, it is necessary to override the hashCode() method, also.
It returns a randomly generated hash code value of the object that is unique for each instance. The randomly generated value might change during the several executions of the program.
The general contract for hashCode is:
Example of hashCode() method
In the following example, we have created two classes Employee.java and HashCodeExample.java.
In the Employee class, we have defined two fields regno of type int and name of type String. After that, we have created a constructor of the Employee class and passes these two fields as a parameter.
To perform the comparison of objects, we have created a separate class named HashCodeExample. In this class, we have created two instances of the Employee class i.e. emp1 and emp2. After that, we have invoked the hashCode() method using objects. We have stored the hash code value in the variable a and b, respectively.
hashcode of emp1 = 2398801145 hashcode of emp2 = 1852349007 Comparing objects emp1 and emp2 = false
Overriding equals() Method
We can override the equals() method in the following way if we want to provide own implementation.
The above code snippet shows that two employees will be equal if they are stored in the same memory address or they have the same regno. When we run the program (HashCodeExample.java) with the above code snippet, we get the following output.
hashcode of emp1 = 2032578917 hashcode of emp2 = 1531485190 Comparing objects emp1 and emp2 = true
Next TopicJava Tutorial