Java equals usage
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: | 4290 |
Bookmark Java equals usage
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:
However if you compare two objects then == compares object location in the memory. Let’s see a basic example:
public class Car {
public String type;
}public class Tester {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
// Set the type value to the samecar1.type = "Lexus";
car2.type = "Lexus";
if (car1 == car2) {
System.out.println("car1 == car2");
} else {
System.out.println("car1 != car2");
}}}
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:
public class Tester {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
// Set the type value to the samecar1.type = "Lexus";
car2.type = "Lexus";
if (car1.equals(car2)) {
System.out.println("car1 equals to car2");
} else {
System.out.println("car1 not equals to car2");
}}}
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:
public class Car {
public String type;
public boolean equals(Object o2) {
if (o2 == null) {
return false;
} else if (o2 instanceof Car) {
return this.type.equals(((Car) o2).type);
} else {
return false;
}}}
Now if we execute our test program again then it reports that the two Car objects are identical.
Extend our object a bit:
public class Car {
public String type;
public String color;
public int age;
public long price;
}
In this case we also need to extend our equals method to compare every class variables in the following way:
public class Car {
public String type;
public String color;
public int age;
public long price;
public boolean equals(Object o2) {
if (o2 == null) {
return false;
} else if (o2 instanceof Car) {
if (this.type.equals(((Car) o2).type)
&& this.color.equals(((Car) o2).color)
&& this.age == ((Car) o2).age
&& this.price == ((Car) o2).price) {
return true;
} else {
return false;
}} else {
return false;
}}}
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 |