Java equals usage


Home - Tutorials - Basics

This article demonstartes how to compare objects in Java using the equals() method.

Tutorial info:


Name:Java equals usage
Total steps:1
Category:Basics
Date:2011-04-11
Level:Beginner
Product:See complete product
Viewed:7966

Bookmark Java equals usage



AddThis Social Bookmark Button

Step 1 - Java equals usage


Java equals usage

Comparing two variables in Java can be resulting interesting results if you are new in Object Oriented programming. The beginners often use the == operator to compare two objects. This is legal and makes sense in some cases but most probably the equals() method should be used.

Let’s see what is the difference between the == and .equals().
In case of primitive data types you can use == to compare their values like this:

Code:
  1. int i = 5;
  2. int j = 5;
  3.  
  4. if (i == j) {
  5.     // Any code
  6. }

However if you compare two objects then == compares object location in the memory. Let’s see a basic example:

Code:
  1. public class Car {
  2.  
  3.     public String type;
  4. }
  5.  
  6. public class Tester {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Car car1 = new Car();
  11.         Car car2 = new Car();
  12.  
  13.         // Set the type value to the same
  14.         car1.type = "Lexus";
  15.         car2.type = "Lexus";
  16.  
  17.         if (car1 == car2) {
  18.             System.out.println("car1 == car2");
  19.         } else {
  20.             System.out.println("car1 != car2");
  21.         }
  22.  
  23.     }
  24. }

As you can see from the output despite of the same type the two object are not handled as identical. This is right as we created two separate object that are sitting on different address in the memory. And this point it’s time to use the equal() method:

Code:
  1. public class Tester {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         Car car1 = new Car();
  6.         Car car2 = new Car();
  7.  
  8.         // Set the type value to the same
  9.         car1.type = "Lexus";
  10.         car2.type = "Lexus";
  11.  
  12.         if (car1.equals(car2)) {
  13.             System.out.println("car1 equals to car2");
  14.         } else {
  15.             System.out.println("car1 not equals to car2");
  16.         }
  17.     }
  18. }

Wait a minute the result is the same as before. How can it happen? This is because the default implementation of equals uses the == operator as well. You need to override equals method. Here is how you can do this:

Code:
  1. public class Car {
  2.  
  3.     public String type;
  4.  
  5.     public boolean equals(Object o2) {
  6.         if (o2 == null) {
  7.             return false;
  8.         } else if (o2 instanceof Car) {
  9.             return this.type.equals(((Car) o2).type);
  10.         } else {
  11.             return false;
  12.         }
  13.     }
  14. }

Now if we execute our test program again then it reports that the two Car objects are identical.
Extend our object a bit:

Code:
  1. public class Car {
  2.  
  3.     public String type;
  4.     public String color;
  5.     public int age;
  6.     public long price;
  7. }

In this case we also need to extend our equals method to compare every class variables in the following way:

Code:
  1. public class Car {
  2.  
  3.     public String type;
  4.     public String color;
  5.     public int age;
  6.     public long price;
  7.  
  8.     public boolean equals(Object o2) {
  9.         if (o2 == null) {
  10.             return false;
  11.         } else if (o2 instanceof Car) {
  12.             if (this.type.equals(((Car) o2).type)
  13.                     && this.color.equals(((Car) o2).color)
  14.                     && this.age == ((Car) o2).age
  15.                     && this.price == ((Car) o2).price) {
  16.                 return true;
  17.             } else {
  18.                 return false;
  19.             }
  20.         } else {
  21.             return false;
  22.         }
  23.     }
  24. }

Note that Objects are compared with equals() but primitive types are compared with == operator.

 






Tags: java equals, compare objects, equal, ==, equal or ==

Java equals usage - Table of contents
Step 1 - Java equals usage



F1 Site Family
AJAX F1
CSS F1
Database F1
HTML F1
Java F1
JavaScript F1
PHP F1

Total time: 0.1598