15
public static void main(String[] args) {
    String str1 = new StringBuilder("计算机").append("软件").toString();
    System.out.println(str1.intern() == str1);
    String str2 = new StringBuffer("ja").append("va").toString();
    System.out.println(str2.intern() == str2);
}

Results:

 true
 false   

First one prints true, and the second prints false. Why are the results different?

icza
  • 389,944
  • 63
  • 907
  • 827
side
  • 209
  • 2
  • 9

4 Answers4

20

The difference in behavior is unrelated to the differences between StringBuilder and StringBuffer.

The javadoc of String#intern() states that it returns

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

The String created from

String str2 = new StringBuffer("ja").append("va").toString();

is a brand new String that does not belong to the pool.

For

str2.intern() == str2

to return false, the intern() call must have returned a different reference value, ie. the String "java" was already in the pool.

In the first comparison, the String "计算机软件" was not in the string pool prior to the call to intern(). intern() therefore returned the same reference as the one stored in str2. The reference equality str2 == str2 therefore returns true.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 1
    This doesn't explain why the other example behaves differently though – Jeroen Vannevel Aug 14 '15 at 01:50
  • 2
    @JeroenVannevel The quote _Otherwise, this String object is added to the pool and a reference to this String object is returned._ isn't good enough? – Sotirios Delimanolis Aug 14 '15 at 01:51
  • 1
    @SotiriosDelimanolis: the other string is interned as well, the only difference is the stringbuilder/stringbuffer and the different character set – Jeroen Vannevel Aug 14 '15 at 01:52
  • 2
    @JeroenVannevel You misunderstand. Character sets are irrelevant. If an equal `String` is in the pool, `intern()` returns a reference to **that** `String`, otherwise it returns a reference to **this** `String`. In the first case, it returned a reference to **this** `String`, which is why `true == (this == this)`. – Sotirios Delimanolis Aug 14 '15 at 01:54
  • 1
    @SotiriosDelimanolis: Ahh so the implicit reason is that "java" is already in there in a brand-new project, even though we haven't added that string ourselves. That's the piece I was missing. – Jeroen Vannevel Aug 14 '15 at 02:01
  • 1
    @JeroenVannevel Yeah, something in the JVM (or through some other bootstrapped class) must be adding it. I wanted to propose that you can verify by simply doing `"java" == str2.intern()`, but that would skew the results as we're now, ourselves, adding it to the pool. – Sotirios Delimanolis Aug 14 '15 at 02:02
  • 3
    @SotiriosDelimanolis: I wanted to verify it to be sure and indeed, a heap dump shows it's in there. http://i.imgur.com/Fv5pbFV.png – Jeroen Vannevel Aug 14 '15 at 02:32
5

Because your assignments don't re-read from the intern pool and Java String(s) are immutable. Consider

String str1 = new StringBuilder("计算机").append("软件").toString();
String str1a = new String(str1); // <-- refers to a different String 
str1 = str1.intern();
str1a = str1a.intern();
System.out.println(str1a == str1);
String str2 = new StringBuffer("ja").append("va").toString();
String str2a = new String(str2); // <-- refers to a different String 
str2 = str2.intern();
str2a = str2a.intern();
System.out.println(str2a == str2);

The output is (as you might expect)

true
true
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Lots of answer before mentioned about the pool and explained really clearly with the Oracle link docs.

I just would like to point out the way we can check when debugging code.

    String str1 = new StringBuilder("计算机").append("软件").toString();
    System.out.println(str1.intern() == str1);//the str1.intern() returns the same memory address the str1
    String str2 = new StringBuffer("ja").append("va").toString();
    System.out.println(str2.intern() == str2);//the str2.intern() does not return the same memory address the str2

You can use any IDE and debug to check the actual address that the str1 and str1.intern()/str2 and str2.intern().

user207421
  • 305,947
  • 44
  • 307
  • 483
Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23
0

let me add something more interesting:

  1. the OpenJDk 8 is true,true;
  2. Oracle JDK 6 is true,true;

so I think the right answer is :

diffrent vendor's jvm or jvm versions may have diffrent implemention(the language specification don't force the how to)

In Oracle JDK 8(I guess u using): String “java” already in pool(loaded by java.lang.Version#laucher_name) and String pool only stores the refrence,not the object.

But in OpenJDK the laucher_name is "openJDK";In Oracle JDK 6 and below ,the string pool will copy the string object to itslef.