Comparing Two Objects in Java

In Java, the Object class serves as the superclass for all other classes and is automatically implemented by every Java class. It offers two crucial methods, namely equals() and hashCode(), which facilitate object comparison. This section will delve into the functioning of these methods and provide illustrative examples of how to compare objects in Java.

In Java, there are two methods provided by the Object class for comparing objects:

  • equals() Method
  • hashCode() Method

equals() Method:

This method is used to compare the equality of two objects. It is a boolean method that returns true if the objects are equal and false otherwise. By default, the equals() method checks for reference equality, meaning it compares the memory addresses of the objects. However, you can override this method in your class to define your custom equality criteria.

Syntax:

public boolean equals(Object obj)

The equals() method takes a reference object as a parameter and returns true if the objects are equal; otherwise, it returns false.

Moreover, the equals() method adheres to the concept of an equivalence relation when comparing objects:

  1. Reflexive: If x is a non-null reference, calling x.equals(x) must always return true.
  2. Symmetric: If x and y are two non-null references, x.equals(y) will return true if and only if y.equals(x) also returns true.
  3. Transitive: If x, y, and z are three non-null references, x.equals(z) will return true if x.equals(y) and y.equals(z) both return true.
  4. Consistent: For any two non-null references x and y, multiple invocations of x.equals(y) should consistently return the same result, either true or false. It should not rely on any information that may change during the comparison process.
  5. For any non-null reference x, x.equals(null) always returns false.

These principles ensure that the equals() method behaves predictably and consistently when comparing objects in Java.

Example of equals() method

In the following example, we have created instances of the Double and Long classes by passing corresponding values as arguments to their constructors.

In the first println statement, we invoke the equals() method and pass another object, y, as a parameter to compare it with the object x. Since x holds a double value and y holds a long value, they are not equal, resulting in the method returning false.

Similarly, in the second println statement, we invoke the equals() method and pass the same value that was used in the constructor of the Double class. This time, it returns true because the Double-object, x, holds the same value as the one we passed in the equals() method.

ObjectComparisonExample.java

public class ObjectComparisonExample  
{  
public static void main(String[] args)   
{  
//creating constructor of the Double class   
Double x = new Double(123.45555);  
//creating constructor of the Long class   
Long y = new Long(9887544);  
//invoking the equals() method   
System.out.println("Objects are not equal, hence it returns " + x.equals(y));  
System.out.println("Objects are equal, hence it returns " + x.equals(123.45555));  
}  
}

Output:

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 is used to compare whether two references are identical or not. On the other hand, the equals() method is used to compare the state of two objects.

When we say objects are equal, it means they have the same state, typically comparing the values of variables within the objects. On the other hand, objects are considered identical when they share the same class identity.

For instance, the expression obj1==obj2 checks for identity, not equality. In contrast, the expression obj1.equals(obj2) compares the equality of the objects by considering their internal state.

hashCode() Method:

In Java, a hash code is a 32-bit signed integer value assigned by the JVM to each Java object. It serves as a unique identifier for the object. Every Java object has an associated hash code, which is utilized by hash-based data structures like HashTable and HashSet.

Syntax:

public int hashCode()

The hashCode() method in Java returns a randomly generated hash code value for an object, which is unique for each instance. It’s important to note that the generated value may change between different program executions.

The general contract for the hashCode() method is as follows:

  1. Consistency: When the hashCode() method is invoked multiple times within the execution of an application, it should consistently return the same hash code (integer value) for a particular object. It’s crucial to ensure that the object being hashed is not modified during this process.
  2. Equality: If two objects are considered equal according to the equals() method, invoking the hashCode() method on both objects should produce the same integer value. This ensures consistency and compatibility between the equals() and hashCode() methods.
  3. Unequal Objects: It is possible that if two objects are unequal according to the equals() method, invoking the hashCode() method on both objects may still produce the same hash code. In other words, the hash code can be the same for distinct objects. This scenario, known as a hash code collision, is acceptable as long as the objects are correctly differentiated by the equals() method.

Here is an example showcasing the usage of the hashCode() method:

Consider two classes, Employee.java and HashCodeExample.java.

In the Employee class, we define two fields: Regno of type int and name of type String. We also create a constructor for the Employee class that takes these two fields as parameters.

To demonstrate object comparison, we create a separate class named HashCodeExample. Within this class, we instantiate two instances of the Employee class: emp1 and emp2. We then invoke the hashCode() method on these objects, storing the hash code values in variables a and b, respectively.

Employee.java

public class Employee   
{  
private int regno;  
private String name;  
//constructor of Employee class  
public Employee(int regno, String name)   
{  
this.name = name;  
this.regno = regno;  
}  
public int getRegno()   
{  
return regno;  
}  
public void setRegno(int Regno)   
{  
this.regno = regno;  
}  
public String getName()   
{  
return name;  
}  
public void setName(String name)   
{  
this.name = name;  
}  
}

HashCodeExample.java

public class HashcodeExample  
{  
public static void main(String[] args)   
{  
//creating two instances of the Employee class  
Employee emp1 = new Employee(918, "Maria");  
Employee emp2 = new Employee(918, "Maria");  
//invoking hashCode() method  
int a=emp1.hashCode();  
int b=emp2.hashCode();  
System.out.println("hashcode of emp1 = " + a);  
System.out.println("hashcode of emp2 = " + b);  
System.out.println("Comparing objects emp1 and emp2 = " + emp1.equals(emp2));  
}  
}

Output:

hashcode of emp1 = 2398801145
hashcode of emp2 = 1852349007
Comparing objects emp1 and emp2 = false

Overriding equals() Method:

To provide a custom implementation for the equals() method, we can override it in the following manner:

//overriding equals() method  
@Override  
public boolean equals(Object obj)   
{  
if (obj == null)   
return false;  
if (obj == this)  
return true;  
return this.getRegno() == ((Employee) obj). getRegno();  
}

The provided code snippet demonstrates that two Employee objects will be considered equal if they are stored at the same memory address or if they possess the same regNo value. When the program (HashCodeExample.java) is executed with the aforementioned code snippet, the output will be as follows:

Output:

hashcode of emp1 = 2032578917
hashcode of emp2 = 1531485190
Comparing objects emp1 and emp2 = true

Follow tutorials.freshersnow.com to expand your knowledge in the topics like Comparing Two Objects in Java.