what is the difference between equals() and == in java

 

In this tutorial we will lean what is the difference between equals() meathod and “==” operator  in java. Let us have a look in to the below example.

 

                                         

 

 

Scenario 1 :

If you see in the below example the out put will be “Both strings s1 and s2 are not equal” why because the content of String objects s1 and s2 are different.

OUT PUT:

 

Scenario 2 :

If you see in the below example the out put will be “Both strings s3 and s4are not equal.” why because the content of String objects s3 and s4 are same but the references of both s3 and a4 are different i.e both s3 and s4 pointing to different addresses because when we use “new” operator it will create new object everytime .

OUT PUT:

 

Scenario 3 :

If you see in the below example the out put will be “Both strings s5 and s6 are equal.” why because the content of String objects s5 and s6 are same and the references of both s5 and a6 are same i.e pointing to same address.

OUT PUT:

 

Happy learning String tutorials …. :)

 

Posted in string and tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">