Monday, 11 April 2016

Difference between == and equals() method in Java - String Object

What is difference between == and equals() method for comparing Objects in Java is one of the classical Interview Questions which appears now and then on many interviews. This question is mostly asked in conjunction with String because comparing String using == and equals() method returns different results. I have often seen as along with other popular String question e.g. StringBuffer vs StringBuilder, Why String is final etc. Java is a pure object oriented language and every object has one state and location in the memory and equals () and == are related with the state and location of the object, now in this article will try to understand this concept and difference between == and equals method in Java.


What is equals method and == operator in Java
equals vs == or eqaulity operator in Java
Both equals() method and == operator is used to compare two objects in Java. == is an operator and equals() is method. But == operator compare reference or memory location of objects in heap, whether they point to same location or not .whenever we create any object using the operator new it will create new memory location for that object. So we use == operator to check memory location or address of two objects are same or not. And when we talk about equals() method main purpose is two compare the state of two objects or contents of the object. But there is one relation between this two isdefault implementation of equals() method  work like == means it will check the memory reference of the object if they point to same location then two objects are equals and it is defined in  Object class .as we know java.lang.Object class is parent for every other object so default  implementation is common for every object but if we want to override the method and want to give own implementation  for checking the equality for two objects we can do, and most of the Java  classes have their own implementation for equals method where they check the contents of the object .

For example  java.lang.String class override the equals() and hashcode method and in overridden method it will check that two string contains same value or character if yes then they are equals other wise not equal.

Difference between == and equals method in Java
Now we know what is equals method, how it works and What is equality operator (==) and How it compare objects, its time to compare them. Here is some worth noting difference between equals() method and == operator in Java:

·          First difference between them is, equals() is a method defined inside the java.lang.Object class and == is one type of operator and you can compare both primitive and objects using equality operator in Java.

·          Second difference between equals and == operator is that, == is used to check reference or memory address of the objects whether they point to same location or not, and equals() method is used to compare the contents of the object e.g. in case of comparing String its characters, in case of Integer its there numeric values etc. You can define your own equals method for domain object as per business rules e.g. two Employes objects are equal if there EmployeeId is same.

·          Third difference between equals and == operator is that, You can not change the behavior of == operator but we can override equals() method and define the criteria for the objects equality.

Let clear all these differences between equals and == operator using one Java example :

String s1=new String("hello");
String s2=new String("hello");

Here we have created two string s1 and s2 now will use == and equals () method to compare these two String to check whetherthey are equal or not.

First we use equality operator  == for comparison  which only returns true if both reference variable are pointing to same object.

if(s1==s2) {
     System.out.printlln("s1==s2 is TRUE");
} else{
     System.out.println("s1==s2 is FALSE");
}

Output of this comparison is FALSE because we have created two objects which have different location in heap so == compare their reference or address location and return false. Now if we use equals method to check their equivalence what will be the output


if(s1.equals(s2)) {
      System.out.println("s1.equals(s2) is TRUE");
} else { 
      System.out.println("s1.equals(s2) is FALSE");
}

Output of this comparison is TRUE because java.lang.String class has already overridden the equals() method of Object class and check that contents are same or not because both have same value hello so they are equal according to String classequals() method .

Point to remember:
If you have not overridden equals() method in  a user defined object,  it will only compare the reference or memory address, as defined in default equals() method of java.lang.Object class and return true only if both reference variable points to same object. So in a user defined class, both equals() and == operator behave similarly but that may not be logically correct and that’s why we should always define the equivalence criteria for custom or domain objects.

That’s all on difference between equals() method and == operator in Java. Both can compare objects for equality but equals()is used for logical and business logic comparison while == mostly for object reference comparison in Java.

Other Java articles and Interview questions from java 67

Hashtable vs HashMap in Java



TAGS :     SQL          SQL TUTORIAL           SQL STUDY ONLINE                 GNIIT HELP

No comments:

Post a Comment