Thursday, 25 June 2015

Difference Between == and equals() method in Java

First of all, the main difference between "==" and "equals()" is "==" is an operator where as equals() is a method.

equals() method is already implemented in Object class. So every new class you write will automatically inherit the equals() method from Object class along with other methods.

"==" With Primitives:

Use == operator if you want to check for equality of primitive type variables, such as short, integer, long, boolean, float, double, byte.

for example,

int a = 5; int b = 5;

System.out.println(a == b); returns true.


Remember, you can't call equals() method on primitive data types, because they are not objects. So for primitives to check for equality, you always need to use '==' operator only. 

 "==" With Reference Variables:

Don't use '==', for testing equality of two objects, for example, let's create two Integer objects, with same values.

Integer a = new Integer(5);

Integer b = new Integer(5);

System.out.println(a==b); returns false.


if you call a == b, it always returns false. Because, it compares addresses of the objects a, b. When ever, you create an object using new operator, it'll always returns a different address. So here you have created two objects with the help of new operator, So object "a" will have different address than object "b" even though the contents of both are same.

"==" with reference variables is also called as "Reference Equality Check".

Let me give one more example, where "==" returns true for two objects.
Integer a = 5;
Integer b = 5;
System.out.println(a==b); returns true.

This is special case. Here, you are not creating an object using new operator. So in the first statement Integer a = 5; it creates an Integer object with value 5.

In the next statement Integer b = 5; It checks whether any object with value 5 is already exits, if exits it'll assign reference of existing object to b. The same happened here.

Remember, when ever you want to check for equality of two Objects, don't use "==" operator, Always use equlas() method only. Read below for more about equals() method.


equals() method :

As mentioned earlier, equals() method is already implemented in Object class, which is a super class to all the Java classes.

equals() method is used to check for equality of contents of two objects, for example,

Integer a = new Integer(5);

Integer b = new Integer(5);


System.out.println(a.equals(b)); returns true.

Here, the equals method checks whether contents of a and b are equal or not, instead of their addresses check. Here it performs, is 5 equals to 5, and returns true.

Let's see the method implementation of equals() method in various classes.

equals() implementation in Object class :
 public boolean equals(Object obj) {

    return (this == obj);

   }

If you see here, it is doing reference check of the objects i.e comparing addresses of objects. Then how "a.equals(b) is returning true?".

Even though, equals() method of Object class compares objects using "==" operator, but this is not the case for every class. for example, Integer class has it's own implementation of equals() method. So when ever you call equals() method on Integer instance, it always calls equals() method of Integer class only, instead of Object class.

Let's see the code of equals() method of Integer class:

equals() implementation in Integer class: 
 public boolean equals(Object obj) {

    if (obj instanceof Integer) {

        return value == ((Integer)obj).intValue();

    }

    return false;

}

Here, it is checking contents equality.

Hope, you understand difference between "==" and equals() method and when to use, when to not use. If you have any doubts, place your comments below.


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.