Saturday 7 March 2015

Difference between equals method and "==" operator & Examples 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 numeric values etc.

        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.

Summary:--

 1) Use == to compare primitive e.g boolean, int, char etc while use equals() to compare objects in
      Java.

 2)  ==  return true if two reference are of same object .  Results of equals() method depends on
    overridden implementation.

 3) For comparing String use equals()  instead of  == equality operator.


Some interview questions & answers based on equals() method and == operator :

1) Can two objects which are not equal have same hashCode?
  
     Yes, two objects which are not equal by equals() method can still returns the same hashCode.    


2) What happens if you compare an object with null using equals() method?

          When null object is passed as an argument to equals() method, it should return false, it must not throw NullPointerException , but if you call equals method on reference, which is  null it will throw NullPointerException. That's why it's better to use == operator for comparing null.


3) What is the output of given code?    


public class StringEx {

       public static void main(String[] args){
            
            String s1 = "Hello";
            String s2 = new String("Hello");
            String s3 = new String("Hello"); 
                                   
            if(s1.equals(s2)) {
                 System.out.println("s1 equals to s2");
            } else {
                 System.out.println("s1 not equals to s2");
            }
                                   
            if(s2.equals(s3)) {
                 System.out.println("s2 equals to s3");
            } else {
                 System.out.println("s2 not equals to s3");
            } 
      }
}

Output:-  s1 equals s2
                 s2 equals s3

   
 4)  What is the output of given code?


public class StringEx {

       public static void main(String[] args){
            
            String s1 = "Hello";
            String s2 = new String("Hello");
            String s3 = new String("Hello"); 
                                   
            if(s1 == s2) {
                 System.out.println("s1 equals to s2");
            } else {
                 System.out.println("s1 not equals to s2");
            }
                                   
            if(s2 == s3) {
                 System.out.println("s2 equals to s3");
            } else {
                 System.out.println("s2 not equals to s3");
            } 
      }
}

Output:-  s1 not equals to s2
                 s2 not equals to s3


Related Post:--
1) String Related Interview Questions & Answers
2) Why String is immutable or final in java?

1 comment:

  1. Hi There,


    Great info! I recently came across your blog and have been reading along.
    I thought I would leave my first comment. I don’t know what to say except that I have

    i have a table called card which got list of card ,now in my java application i got validation which what certain card to be validated e,g mastro,visa,amex etc now i have problem when i create new card its
    throwing error because the new card is not in the hard coded values,the new card is not affected by the validation,any advice how to approch this






    Awesome! Thanks for putting this all in one place. Very useful!


    Many Thanks,
    Irene Hynes

    ReplyDelete