Saturday 13 April 2013

Difference between using == operator and equals() method in Java

This is a very basic question and most frequently asked in interviews.  It is very important to understand the difference before you write complex Java programs.

   Let us start with == operator first.We know this is a comparator operator but the question is how does it actually work and how is it different from .equals() method?

== operator

       == operator compares whether the two references that are compared point to the same object or not. Result will be true if both references point to the same object else it is false.Lets take an example to understand this better.

package testCodes;

public class Test {
   
    public static void main(String args[])
    {
        String name1 = new String("John");
        String name2 = name1;
        if(name1 == name2)
        {
            System.out.println("Both references point to the same object \n");
        }

        else

        {   

            System.out.println("Both references point to different objects \n");

         }
    }
}

output : Both references point to the same object

In above code we create a String object name1 with value John. Then we create a reference name2 and make it point to the same object created earlier. Then we compare both references and as both references point to same object with value John == operator return true and hence we get our desired output. Lets say we do something like below -

String name1 = new String("John");

String name2 = new String("John");

if(name1 == name2)

{
    System.out.println("Both references point to the same object \n");

} 

else

{   

    System.out.println("Both references point to different objects \n");

}


 output : Both references point to different objects

As expected if we create two different objects, though both contain the same value John we get false from == operator as references point to two different object and else part is printed.

Note if you do something like

String name1 = "John";

String name2 = "John";

if(name1 == name2)

{
    System.out.println("Both references point to the same object \n");

} 

else

{   

    System.out.println("Both references point to different objects \n");

}


 output : Both references point to the same object

This is because when you don't create explicit objects, Java internally stores this data in a pool on the heap and any string created with  John as the value will point to this data in the pool.This is data is stored in an object called interned String objec. Hence they refer to same object and == return true. For more details refer the tutorial on Difference between String Object and String Literal.

To sum this up == operator check whether the two references which are being compared point to same object or not.

.equals() method

     This method is very much straight forward. It will simple check if the two objects are internally same or not. In case of String it will check if both objects have same value or not. If you are creating an object of your own you must override this  method to handle equality.

The default implementation of .equals() method is as follows -  

public boolean equals(Object other) 
     return this == other;   

Yes that's right! By default .equals() method is same as using == operator. Hence for custom classes you must override this method by adding logic to compare  your objects.

For example if you are making a Coordinate class which has x and y coordinates as instance variable you may override .equals() method as follows - 


public boolean equals(Object other) 
     if((Coordinate)other.x == this.x  && (Coordinate)other.y == this.y)
     {
         return true;
      }
    else
     {
        return false;
      } 

Above code just compare if x coordinate and y coordinate of both coordinate objects are same. If yes then the objects are similar and true is returned else false is returned. I hope you get the point.Also don't forget to override hashCode()method. We will learn this hashCode() method and why to override this in next tutorials but for now just keep that in mind.



Summary


    == operator check whether both the objects compared point to the same object or not while .equals() method is overridden and implemented by the programmer. .equals() method is provide to provide functionality to compare similarity of two objects. If it is not overridden then it is same as using ==  operator.


Related Links




2 comments:

t> UA-39527780-1 back to top