What is the use of == and equals() method?

Showing Answers 1 - 3 of 3 Answers

Guest

  • Mar 21st, 2006
 

The == operator compare whether two object references are refer to the same instance or not.

The equals() method  compare the characters(contents) of two String object.

example:

        String a="Hai";                       
        String b="Hai";
        String c=new String("Hai");
        System.out.println(a==b);           //True
        System.out.println(a==c);           //False
        System.out.println(a.equals(b));   //True
        System.out.println(a.equals(c));   //True

  Was this answer useful?  Yes

Anurag Kushwaha

  • Mar 25th, 2006
 

  Ya!!!

  absolutely correct.........

  Was this answer useful?  Yes

Pramod Chakravarthy Kalwa

  • Jun 17th, 2006
 

== and equals() checks the references by default, but its not the same with the String's and Wrapper classes because the equals()(method in object class) is overidden in java.lang.Sting class and Wrapper Classes to check the content of the object.Except for String and Wrapper classes both == and equals() method returns the same boolean value..(either true or false)..u can check this with the following programme..public class Abc{ public static void main(String aargs[]) { String s1=new String("abc"); String s2=new String("abc"); if(s1==s2) System.out.println("Equals"); else System.out.println("Not Equals"); if(s1.equals(s2)) System.out.println("Equals"); else System.out.println("Not Equals"); }}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions