Java equals

Java Equals

Java Equals

We understand how hash code works in java programming. Now we will understand how equals() and hashcode() methods work together.


Java Equals



So Let’s get started.


Java Equals

We already know that we have “==” operator for comparing the values in java then why do we need a separate method for comparison. To understand the different between “==” and equals() method, we need to implement an example as below.
public class Test {

 public static void main(String[] args) { 
   String s1 = new String("Ashish");
   String s2 = new String("Ashish");
   System.out.println((s1 == s2));

 }

}
Output : false

What just happened in above example, why is it giving false output. Look at the strings, they both have same content. It should be true, there is something wrong here.

Actually nothing wrong is happening. The “==” works well with the primitive values like int, float, double etc, when you compare those values their content will be checked and result will be provided as supposed.

But when “==” is being used with the objects than everything changes. The “==” operator does not compare the values of the objects rather it compares the reference of the object, whether they refer to the same object or not. If both objects are different like in the case of above example where we have two different objects than value will be false.

When you really need to know if two references are identical, use “==”. But when you need to know if the objects themselves (not the references) are equal, use the equals() method.

That’s why java provided us separate method to compare the values of the object as per our requirement that is “equals()”.

For each class, you will have to override the equals() method to decide which property of an object makes sense to be used for comparison. Some objects can never be equal like in the case of Car object, each car has it’s own model, number and other properties. If you want to perform search on the list of cars then each car must be different from other cars otherwise you will get unexpected results.


What will happen if you don’t override the equals() method


Internally equals method uses “==” operator for comparison by default, so unless you don’t override the equals method both objects will be considered equal if they refer to the same object.

let’s consider the above example of Car object. Suppose you have a list of Car objects and each car is unique, they are different from one another. Now you want to search a particular car from the given list of Car objects, you simply perform equals() operation on the list.

What will happen, you will get true for each of the car. Because you haven’t overridden the equals() method of Car class, now each Car will be equal as default “==” operation will be performed for comparison.

Let’s implement equals() method

Now let’s understand how you can implement equals method in your code to enable comparison of your objects properly.

Let’s comment the equals part and see the output for Car object which has the same car number.

class Car {
   private String carNumber;
 
   public Car(String carNumber) {
     this.carNumber = carNumber;
   }
 
   public String getCarNumber() {
     return this.carNumber;
   }
 
   /*
   public boolean equals(Object o) {
     if((o instanceof Car) && ((Car)o).getCarNumber() == this.getCarNumber()) {
       return true;
     } else {
       return false;
     }
   } */
}

public class Test {

 public static void main(String[] args) { 
 
   Car c1 = new Car("AA893");
   Car c2 = new Car("AA893");
 
   if(c1.equals(c2)) {
     System.out.println("Both cars are equal.");
   } else {
     System.out.println("They are different.");
   }
 }
}
Output : They are different.


Even if the car number is same still output is both cars are different. Reason is that if we don’t override the equals method then default “==” equal operation will be performed.

Now remove the comment and see the equals in action after being overridden.

class Car {
   private String carNumber;
 
   public Car(String carNumber) {
     this.carNumber = carNumber;
   }
 
   public String getCarNumber() {
     return this.carNumber;
   }
 
   public boolean equals(Object o) {
     if((o instanceof Car) && ((Car)o).getCarNumber() == this.getCarNumber()) {
       return true;
     } else {
       return false;
     }
   }
}

public class Test {

 public static void main(String[] args) { 
 
   Car c1 = new Car("AA893");
   Car c2 = new Car("AA893");
 
   if(c1.equals(c2)) {
     System.out.println("Both cars are equal.");
   } else {
     System.out.println("They are different.");
   }
 }
}
Output : Both cars are equal.


Now see the difference what had expected from the equal operation on the car object. If both cars have the same number then they should be the same.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *