Please navigate to the bottom of the page for Table of Contents

Sunday, June 19, 2011

JAVA equals() versus == operator

Question: Show the difference between == and equals() method with an example.

The comments in the program below are self explanatory and should help you figure out the subtleties of new object creation and the == operator versus equals() method.

public class CompareQuestion 
{
    public static void main(String[] args) 
    {
        String s1 = new String("A"); //not recommended, use String s1 = "A"
        String s2 = new String("A"); //not recommended, use String s2 = "A"
    
        //standard: follows the == and equals() rule like plain java objects.
        if (s1 == s2) 
        {   //shallow comparison, never reaches here
            System.out.println("references/identities are equal"); 
        }
    
        if (s1.equals(s2)) 
        {   //deep comparison, this line is printed
            System.out.println("values are equal"); 
        }
    
        //variation: does not follow the == and equals rule
        String s3 = "A"; //goes into a String pool.
        String s4 = "A"; //refers to String already in the pool.
        if (s3 == s4) 
        {   //shallow comparison, this line is printed
            System.out.println("references/identities are equal"); 
        }
    
        if (s3.equals(s4)) 
        {   //deep comparison, this line is also printed
            System.out.println("values are equal"); 
        }
    }
}

Note: When you create a new string object by using the new String(“A”), a new String instance is created each time it is executed.

18 comments:

  1. Ever since their invention computers have become an essential part of our lives. We use computers and computer related devices in everyday life. They have acquired supreme importance in every walk of life.
    StartPoint

    ReplyDelete
  2. Hi There,

    What a brilliant post I have come across and believe me I have been searching out for this similar
    kind of post for past a week and hardly came across this.

    I have two methods and they do almost same thing, but at some most inner nested level one of method have additional command.

    Java Code:
    method2(){
    ... same code ...
    for {
    ... same code ...
    if{
    ... same code ...
    }
    }
    }
    Follow my new blog if you interested in just tag along me in any social media platforms!

    Shukran,
    Munni

    ReplyDelete
  3. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work... Kopar at Newton Condo

    ReplyDelete
  4. The most important thing that a new programmer needs to know is that the "Learn Programming in 24 hours" sort of books are simply not true. A more accurate title would be "Learn Programming in 10,000 hours".best ergonomic chair

    ReplyDelete
  5. Here at this site really the fastidious web design studio material collection so that everybody can enjoy a lot.

    ReplyDelete
  6. You can do this by finding the module in the WordPress Module Catalog and checking the similarity confine the correct hand section. error establishing a database connection

    ReplyDelete
  7. It will be essential that each progression of advancement stake its natural incentive to society via flawlessly sticking to social, environmental, and financial boundaries. excel vba training london

    ReplyDelete
  8. But the web developers use a variety of add-on modules to write modern web applications in Python. While writing web applications in Python, programmers have option to use several high-level web frameworks unindent does not match any outer indentation level python

    ReplyDelete
  9. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work. targonca szállítás Debrecen Europa-Road Kft.

    ReplyDelete
  10. It's not my very first time to visit this blog; I’m visiting this daily and acquire superb info from here day by day.
    app designing company

    ReplyDelete
  11. Nobody can reject the info you have given in the blogs, this is actually a great work.
    UI design firms

    ReplyDelete
  12. The HP ENVY Pro 6455 is an all-in-one printer that performs like most models in HP's ENVY lineup. It's rather compact and feels fairly well-built overall. However, its cartridge system yields a very low number of pages, so you may have to buy new cartridges quite often, resulting in a disappointing cost-per-print. read more this article

    ReplyDelete
  13. informative blog , keep posting and dont forget to checkout our blog full stack course in satara

    ReplyDelete