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 :

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

Q:- What happens if you compare an object with null using equals() method?
 Answer:- 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.

Q:- 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");
                                   }
                              }
                        }
 Answer:-  s1 equals s2
                 s2 equals s3
   
 Q:- 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");
                                   }
                              }
                       }
Answer:-  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?

No comments:

Post a Comment