1

Possible Duplicate:
Comparing two strings with “==”: when will it work?

I have the following code:

String s1 = new String("Test");
String s2 = new String("Test");

String s3 = "Test";
String s4 = "Test";

System.out.println(s1 == s2);
System.out.println(s3 == s4);

And I get the following output:

false
true

Why does the second comparison print true?

Community
  • 1
  • 1
  • 9
    Possible duplicate: [Comparing two strings with “==”: when will it work?](http://stackoverflow.com/q/7479334/844882) – Alex Lockwood May 23 '12 at 18:01
  • it is the same issue, but the answer there is super confusing - does another, simpler answer here make sense? – mfsiega May 23 '12 at 18:03
  • @mfrankli I wouldn't call a very precise and in-depth answer confusing, at least not without qualification. It may be too advanced for a beginner, that I admit. – Marko Topolnik May 23 '12 at 18:21

3 Answers3

5

The correct way to compare a String is with,

 s1.equals(s2)
  1. System.out.println(s1==s2) prints false, because s1 and s2 are pointing to different objects on the heap.

  2. System.out.println(s3==s4) prints true, because s3 and s4 are object references which are pointing to the same object on the heap.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 2
    This is incorrect. `s3` and `s4` are **not** on the same heap as `s1` and `s2`. They both point to a `String` constant in the **String literal pool**. – Alex Lockwood May 23 '12 at 18:12
  • i never said s3 and s4 are on the Heap, please read it again, I said that they are pointing on the same object on the heap...... – Kumar Vivek Mitra May 23 '12 at 18:17
  • Usually, "the heap" refers to the more general heap that is responsible for dynamic memory allocated using the `new` operator. In my answer, I would have mentioned either the String literal pool or String internalization, just to be as clear as possible :). – Alex Lockwood May 23 '12 at 18:20
  • Now if your are taking abt heap, let me tell you that Heap has 2 areas, 1. Object Space 2. Memory Area . Memory Area is the one where String pool resides, and various system classes, including the custom classes, and everything here is in byte form... – Kumar Vivek Mitra May 23 '12 at 18:23
  • Yes, sorry... I shouldn't have said it was "incorrect". I still would have mentioned the String literal pool in my answer though. It still seems to me that you are getting the upvotes, so I doubt I did too much damage :P. – Alex Lockwood May 23 '12 at 18:33
3

Not sure it answers the question but :

== is used to compare the reference (the pointers)

so s1==s2 don't check if the string are the same but if s1 and s2 are the same String instance.

To check equality you must use the equals function : s1.equals(s2)

As string are internalized, the behavior depends on the jvm and cannot be predicted (even if most implementations are the same)

Vinze
  • 2,549
  • 3
  • 22
  • 23
  • 1
    It can be predicted if you know which JVM version you are using... – Alex Lockwood May 23 '12 at 18:15
  • Of course, the last sentence miss an implicit "unless you know only one JVM implementation will be used"... best practice however would be not to rely on a specific implementation... – Vinze May 23 '12 at 18:18
  • Hmm... are you about this? String interning can (and usually does) significantly improve efficiency and memory usage. I can't imagine a JVM that doesn't automatically intern strings... – Alex Lockwood May 23 '12 at 18:31
2

In Java, it is important to note, == always means "return true if these two things refer to the same instance of an object in memory"

When you declare a string using the constructor:

String s1 = new String("Foo");
String s2 = new String("Foo");

A new string instance is always created. This means that even though they have the same value, s1 and s2 are never going to be the same object and as such == returns false.

There is a special case for constant strings however. When you declare a string thus:

String s3 = "Foo";
String s4 = "Foo";

then a single constant string is created in the string pool, and both variables s3 and s4 point to it as a memory optimization. This should be treated as an implementation detail however. Comparison should still always be done using the Equals method.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125