Here is a java fragment:
public class TestIntern {
public static void main(String[] argvs){
String s1 = new StringBuilder("ja").append("va").toString();
String s2 = new StringBuilder("go").append("lang").toString();
System.out.println(s1 == s1.intern());
System.out.println(s2 == s2.intern());
}
}
And it behaves differently depending on different JDKs
in Oracle JDK 1.7, the output is:
false
true
in the output of OpenJDK 1.6 also:
false
true
but in Oracle JDK 1.6 the output is:
false
false
since the JavaDoc for this method String#internindicates
* 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.
~~~~
And what does *this* here mean? the string object
in the heap or in the string pool? if it returns
object in the heap the out put should be:
true
true
otherwise should *always* be:
false
false
Am I right?
conclusion:
true
true
to be expected, but none of these JDKs do this. and why Oracle JDK1.6 gives:
false
false
as a result?
I think that in OracleJDK 1.7 and openJDK 1.6 there should be some kind of reserved row in the row pool, and what is it? Does the document contain all reserved lines? Really confused.
source
share