22 May, 2014

What is the difference between "==" and .equals() Method ?

While programming it is a quite common condition in which we have to equate two objects. That is to identify that two objects are equal or not.
To find out equality Java provides us two options
§ By Using Operator ==
§ By Using Method equals
To understand which should be used when, let us think as coders of Java language would have. Assume that we are going to write the functionality for the operator "==".
Our aim is to identify that is L.H.S equal to R.H.S or not. Another target that we need to achieve is that this operator should work on all kinds of objects, including those which are not even defined today.
How can we equate two objects, whose classes are not even written till date? There has to be a factor on which we can decide if the objects are equal. There is one, and only one such scenario in which we can be sure that L.H.S is equal to R.H.S, and that is when both the objects are placed on same memory location.
And this is exactly what Java guys did. They wrote the code for this operator to match the references (Memory location) on both the sides. If they refer to same memory location, they are equal else not.
If == operator solves our purpose, then what is the use of equals method? In fact, if you check the class Object in java, equals method does nothing more than using == operator.

 public boolean equals(Object obj) {   
        return (this == obj)
 }
Calling equals method is only adding a method call.
Consider a scenario, you have two objects of same class, having exactly same values of member variables but since they are present on different memory locations, == operator will return 'false' for them.
What will equals method return? (If you said 'true', wait my tiger)
I will simply say that when equals method is called, its definition will be executed.
Where is the definition present? It is in the Object Class. So the equals method will still return 'false' as it is simply calling == operator.
So what is the solution, if we want to equate two objects based on the values inside,and not on the basis of its memory location?
Remember the concept of method overriding? It is the technique of hiding method definition in parent class by providing a new method definition with same signature in child class. So making use of method overriding we will give the definition of equals method, and it will behave according to this new definition. One fine example can be seen in String class
The equals method here, equates two objects on the basis of there values and ignores the memory location they are on.
So for String class
String first = new String ("test"); // creates a new string object with value "test"
String second= new String ("test");// creates another string object with value "test" at different memory location
first.equals(second) // returns true as values are compared
first==second// returns false as memory location is compared.
This was good for String class, only because  String defines its own equals method.  Point to be noted here is for any class which does not defines equals method of its own, the method definition will still be available (From top most parent class i.e. Object). And for such classes equals method will behave exactly similar to == operator.