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. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
    Thanks for sharing !
    tanki online 2 | 2048 game online

    ReplyDelete
    Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. 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
  3. 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
  4. 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
  5. 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
  6. Here at this site really the fastidious web design studio material collection so that everybody can enjoy a lot.

    ReplyDelete
  7. 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
  8. 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
  9. 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
  10. 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
  11. Single purpose German Shepherds can be used as patrol or detection dogs. We sell these dogs to law enforcement all over the world.

    Our dogs all have high drives to work for the handler or trainer.

    All our single purpose german shepherd police dogs for sale are delivered with an international passport that holds vaccination records.

    ReplyDelete
  12. 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