1

Possible Duplicate:
How do I compare strings in Java?

why first comparison ( s1 == s2 ) displays equal whereas 2nd comparison ( s1 == s3 ) displays not equal....?

    public class StringComparison
    {

         public static void main( String [] args)
         {
              String s1 = "Arsalan";
              String s2 = "Arsalan";

              String s3 = new String ("Arsalan");

              if ( s1 == s2 )
                 System.out.println (" S1 and S2 Both are equal...");
              else
                 System.out.println ("S1 and S2 not equal");

              if ( s1 == s3 )
                 System.out.println (" S1 and S3 Both are equal...");
              else
                 System.out.println (" S1 and S3 are not equal");

         }
     }
Community
  • 1
  • 1
Arsalan
  • 513
  • 1
  • 7
  • 22

7 Answers7

11

This has to do with the fact that you cannot compare strings with == as well as compiler optimizations.

== in Java only compares if the two sides refer to the exact same instance of the same object. It does not compare the content. To compare the actual content of the strings, you need to use s1.equals(s2).

Now the reason why s1 == s2 is true and s1 == s3 is false is because the JVM decided to optimize the code so that s1 and s2 are the same object. (It's called, "String Pooling.")


Per 3.10.5: Pooling of string literals is actually mandated by the standard.

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

Mysticial
  • 464,885
  • 45
  • 335
  • 332
4

Don't use == to compare strings, it tests reference equality (do two names refer to the same object). Try s1.equals(s2);, which actually tests the elements for equality.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • Yes u r correct but i just want to know that what is the reason behind this.....? – Arsalan Nov 05 '11 at 06:33
  • @Arsalan since s1 and s2 have the same content they can share the object to save space; Objects in Java are as shared as possible – manuzhang Nov 05 '11 at 06:55
1
String one = "Arsalan";
String two = "Arsalan";
  1. one == two

    // Returns true because in memory both Strings are pointing to the SAME object

  2. one.equals(two)

    // Will ALWAYS return true because the VALUES of the Strings are the same (would not matter if the objects were referenced differently).

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
1

The reason is string interning. It's complicated. The compiler is "smart" enough to use the same exact object for s1 and s2, even though you might think they are different. But s3, which uses new String("Arsalan"), doesn't intern.

Some guidelines:

  1. You should almost always use equals(), not ==, to compare strings
  2. You should almost never use String s = new String("foo"). Instead, use String s = "foo".
user949300
  • 15,364
  • 7
  • 35
  • 66
1

If "Arsalan" is not found in the pool of Strings, a "Arsalan" string will be created and s1 will refer it. Since "Arsalan" string already exists in the pool of Strings s2 will refer to the same Object as s1. Because the new keyword is used for s3, Java will create a new String object in normal (nonpool) memory, and s3 will refer to it. This is the reason why s1 and s3 don't refer to the same object.

Seitaridis
  • 4,459
  • 9
  • 53
  • 85
0

Don't use ==, use s1.equals(s2) or s1.equals(s3) instead.

chiwangc
  • 3,566
  • 16
  • 26
  • 32
mdominguez
  • 13
  • 4
0
public class StringComparison
{

     public static void main( String [] args)
     {
          String s1 = "Arsalan";
          String s2 = new String("Arsalan");

          String s3 = new String ("Arsalan");

          if ( s1 == s2 )
             System.out.println (" S1 and S2 Both are equal...");
          else
             System.out.println ("S1 and S2 not equal");

          if ( s1 == s3 )
             System.out.println (" S1 and S3 Both are equal...");
          else
             System.out.println (" S1 and S3 are not equal");

          if ( s2 == s3 )
             System.out.println (" S2 and S3 Both are equal...");
          else
             System.out.println (" S2 and S3 are not equal");

     }
 }

If you run this, you can see that S2 and S3 are also not equal. This is because s2, s3 are references to a String Object and hence they contain different address values.

Ananth
  • 4,227
  • 2
  • 20
  • 26