String comparison with equals and assignment operator

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all,

In this article we are going to discuss about a very common mistake regarding string comparison. This is made by a lot of programmers and of course they suffer the consequences for doing that. You have heard that objects are compared with ==  and  equals()  method. But what is the difference between them exactly? Let us know it with the help of a simple program.

We expect this to output as true but it outputs false . Why is that?

  • The ‘== ‘ operator returns true if the reference variables refer to the same object otherwise it returns false.

In our case the statements String aName = new String("prasad"); and String anotherName = new String("prasad"); will respectively create two new object in the memory. Of course these two are different object and hence the variables aName and anotherName are referring to different objects so aName == anotherName returns false

Now we know that strings should not be checked for equality using == operator, but how to compare them then?

  • The public boolean equals(Object o) method overridden for String class checks for the contents of the strings to be compared.

We will modify the program as follows.

This will print true because the contents of both objects are same. i.e. “prasad”.

Always remember. Whenever you are comparing strings for equality, then use equals method. Do not use ‘==’ operator if you don’t want your life to be screwed and suffer alone crying in the darkness.

Well if we modify our program as follows,

This will print

Now what is that? ‘==’ operator is also printing true, keep scratching your head until you find an answer in these links.

http://www.thejavageek.com/2013/06/19/the-string-constant-pool/

http://www.thejavageek.com/2013/07/25/object-references-and-assignment/

 

 

 

Share Button

7 comments for “String comparison with equals and assignment operator

  1. Sudhir Kumar
    November 27, 2013 at 1:08 am

    1.) how i can calculate the same no of character in String . 2.) finding the minimum length of sub-string between two same char if found in String OR, finding duplicate character in String , etc. so how i can solve this type of Question.

    This type of Question, I tried to solve using simple code like – multiple for-loop, if condition,etc. that was big code. i think they are looking for some other code . so what i should do. i have to used API(how i can get it) / is there any predefined methods for this. how i can solve it ?.

    • Prasad Kharkar
      November 27, 2013 at 10:38 am

      String class provides all these methods. You can check them in official javadoc

  2. December 10, 2013 at 4:30 pm

    A way far helpful post…! Keep up the good work

  3. Akash
    November 7, 2014 at 11:21 pm

    What is the difference between

    1) public class StringComparison {
    public static void main(String[] args) {
    String aName = new String(“prasad”);
    String anotherName = new String(“prasad”);
    System.out.println(aName == anotherName);
    }
    }

    2)public class StringComparison {
    public static void main(String[] args) {
    String aName =”prasad”;
    String anotherName = “prasad”;
    System.out.println(aName == anotherName);
    }
    }

    • November 10, 2014 at 8:39 am

      First will print false because two new string objects are being created. Second case will print ‘true’ because only one object is being referenced that is in string constant pool.
      The operator ‘==’ checks for references.

  4. maen
    November 16, 2016 at 2:58 am

    I don’t get it. Shouldn’t in the very last codeblock the first line be false, the second line be true???

Leave a Reply

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