Why are the results of of str == str.intern() for these strings different?
Asked Answered
A

4

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?

Atworth answered 14/8, 2015 at 1:25 Comment(4)
docs.oracle.com/javase/7/docs/api/java/lang/…Ichthyo
sorry! I dont understand whats your point?Atworth
Why not? There's no reason why either of them should return true, or why they should both return the same result.Mcgaha
@Atworth Please use apostrophes instead of back ticks, unless you want to highlight code.Encephalography
C
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.

Circumcise answered 14/8, 2015 at 1:38 Comment(7)
This doesn't explain why the other example behaves differently thoughStumpy
@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?Circumcise
@SotiriosDelimanolis: the other string is interned as well, the only difference is the stringbuilder/stringbuffer and the different character setStumpy
@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).Circumcise
@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.Stumpy
@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.Circumcise
@SotiriosDelimanolis: I wanted to verify it to be sure and indeed, a heap dump shows it's in there. i.imgur.com/Fv5pbFV.pngStumpy
C
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
Copro answered 14/8, 2015 at 1:37 Comment(2)
so that means str1 and str1a have the same referenceAtworth
@Atworth Any time two Object(s) are equal with == they have the same reference.Copro
T
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().

Tenter answered 14/8, 2015 at 1:59 Comment(0)
O
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.

Onaonager answered 15/10, 2018 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.