7

Possible Duplicate:
Java Strings and StringPool

I created two Strings

String s1="MyString";
String s2=new String("MyString");
System.out.println(s1==s2);

it prints "false" . we know that String pool doesn't create two Objects for same String literal.

Then what is happening here? It's creating two different String Objects(literals) in String pool for same String literal "MyString".

I know equals() method returns true here.

but when we use == it should compare two references and they should refer to the same String Object in String constant pool.

Why It is not refering to the existing String object in the String pool even if it finds a match ?.

Community
  • 1
  • 1
Raju Boddupalli
  • 1,789
  • 4
  • 21
  • 29

5 Answers5

14

The first one goes to the pool while the second one is stored on the heap.

Use s2 = s2.intern(); to make it return true.

When you do an intern() on the string, the JVM ensures that the string is present in the pool. If it doesn't yet exist, it is created on the pool. Otherwise, the already existing instance is returned. I think this explains the == behaviour.

String s1="MyString";
String s2=new String("MyString");
s2 = s2.intern();
System.out.println(s1==s2);

As a reference, here is what the String.intern() documentation says:

/**
 * Returns a canonical representation for the string object.
 * <p>
 * A pool of strings, initially empty, is maintained privately by the
 * class <code>String</code>.
 * <p>
 * When the intern method is invoked, if the pool already contains a
 * string equal to this <code>String</code> object as determined by
 * the {@link #equals(Object)} method, then the string from the pool is
 * returned. Otherwise, this <code>String</code> object is added to the
 * pool and a reference to this <code>String</code> object is returned.
 * <p>
 * It follows that for any two strings <code>s</code> and <code>t</code>,
 * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
 * if and only if <code>s.equals(t)</code> is <code>true</code>.
 * <p>
 * All literal strings and string-valued constant expressions are
 * interned. String literals are defined in section 3.10.5 of the
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * @return  a string that has the same contents as this string, but is
 *          guaranteed to be from a pool of unique strings.
 */
public native String intern();
adarshr
  • 61,315
  • 23
  • 138
  • 167
6

In the line String s2=new String("MyString"); you are creating a new instance of String, so it will surely won't be the same instance as s1.

If you do:

System.out.println(s1=="MyString");

You will get true.

MByD
  • 135,866
  • 28
  • 264
  • 277
3

s2 is NOT a string literal, it was constructed from one when using new - but it is not a literal, it is a new object.

amit
  • 175,853
  • 27
  • 231
  • 333
3

Actually you are creating two different objects one is in Literal Pool and one is in Heap

String s1 = "MyString"; //literal pool
String s2 = new String("MyString"); //in heap
s.o.p(s1==s2); // false, because two different instances
s.o.p(s1.equals(s2)); // true, compares the contents of the string.

You need to use equals instead ==. You can push the string objects from heap to String literal pool (or constant pool) by calling String.intern().

RP-
  • 5,827
  • 2
  • 27
  • 46
0

The String pool is not creating two instances, it is the constructor calls that creates the second String instance. From the javadoc of String(String original):

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

Consequently, since you are creating a second instance == correctly returns false (and equals(Object anObject) will return true).

matsev
  • 32,104
  • 16
  • 121
  • 156