Today, when reading a book encountered such a problem, for the String.intern () method has some knowledge and views. First we look at its API
The idea is that the Intern () method records the first occurrence of an instance reference in a constant pool, but not in jdk1.6. The string instance that will be first encountered in jdk1.6
A copy is placed in the persistence generation, and the return is a reference to the string in the permanent generation.
Such as
First, the running environment is jdk1.7, and will be true and false after running. Because StringBuilder creates instances on the Java heap, the Intern () method implementation will no longer replicate the instance.
This is true only if the first occurrence of the instance reference is recorded in a constant pool. S2 returns false because the string "Java" has already occurred before the StringBuilder () method is executed.
This means that the default in the string constant pool already has its reference, no longer conforms to the "first occurrence principle", and therefore returns false. and the "computer Software" string is the first occurrence, so
Returns true. The detail point is that the reference to the "Java" string has long been available. Here is a reference to the book "in-depth understanding of Java virtual Machines". I hope you can exchange more ideas, thank you for watching.
Some attention to Java's Sting.intern ()