How to Compare Two Objects in JavaJava 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() MethodThe 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. Syntax: 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() methodIn 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. ObjectComparisonExample.java Output: Objects are not equal, hence it returns false Objects are equal, hence it returns true Difference Between == Operator and equals() MethodIn 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() MethodIn 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. Syntax: 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() methodIn 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. Employee.java HashCodeExample.java Output: hashcode of emp1 = 2398801145 hashcode of emp2 = 1852349007 Comparing objects emp1 and emp2 = false Overriding equals() MethodWe 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. Output: hashcode of emp1 = 2032578917 hashcode of emp2 = 1531485190 Comparing objects emp1 and emp2 = true Next TopicJava Tutorial |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India