Witscale Test Center

13.6 The java.lang.Object class > 13.6.2 The equals() method in Object, String and wrapper classes


13.6.2 The equals() method in Object, String and wrapper classes

Java provides two ways for comparing objects the == (and != ) operator and the equals() method. We have already seen how == and the equals() method  work in String comparison (chapter 9 Operators). In the following section, we will quickly overview the object comparison with == and equals() and also the difference between two.

The  == operator

The == compares the objects by comparing the object references. Since object references stores a reference to object which is unique, they are same only when they both are referencing the same object. Therefore the == operator can return true only when you compare the references of essentially the same object. You can use == to compare primitive values as well.

The equals() Method

The equals() method compares the objects by comparing their contents.  Though the equals() method in Object class behaves exactly same as == operator, the subclasses of Object often  override it to implement their own interpretation of equality. Many subclasses override the equals() in such a way that it will return true if the contents of two objects are same. Remember equals()is used only to compare objects. You obviously cannot invoke it on primitives. However, you can invoke it on wrapper objects.

Wrapper classes and equals() method

All wrapper classes override the equals() method to compare the wrapped values instead of object references. For that the overridden equals() in wrapper class follows two steps-

1.       It checks whether the wrapper object passed as argument is of same type as the current object. Note the equals() method takes object of Object type as argument. Therefore, object of any type can be passed to it. Therefore, the equals() method must make sure that the object passed is approprite type. For instance, the equals() method in Integer class checks first whether the object is of type Integer.

2.       If the type is correct then the wrapped value in the current object and wrapped value of the object passed as argument are compared If these values are same, teh equals() will return true. For instance, the equals() method of class Integer is as follows:

 

public boolean equals(Object obj) {

     if (obj instanceof Integer) { //1. Compare type

       return value == ((Integer)obj).intValue();  //2. Compare value

     }

     return false;

}

 

Therefore when you compare the two wrapper objects with equals() method , they are said equals when their type and wrapped value, both are same.

 

You can invoke the equals()  method to compare two Integer objects as:

 

Integer i1 = new Integer(5);        // i1 wraps the value 5

Integer i2 = new Integer(5);        // i2 wraps the value 5

System.out.println(i1.equals(i2));  // prints true

 

Note i1 and i2 are pointing to two separate objects in memory. Hence, if you compare the object references i1 and i2 with == they will not be equal. For instance,

 

Integer i1 = new Integer(5);       // i1 wraps the value 5

Integer i2 = new Integer(5);       // i2 wraps the value 5

System.out.println(i1 == i2));     // prints false

 

The comparison with == cannot see that the i1 and i2 are referencing two objects with exactly same content. Hence overriding the equals() is very important for object comparison. All the other classes including Boolean and Character override the equals() method. Note that the wrapper classes are final, hence all their methods are also final. Therefore the equals() method in wrapper classes cannot be overridden further.