It seems that JDK7 handles the experience differently, as before.
I tested it with line 1.7.0-b147 and got "both are equal", but when I execute it (the same bytecode) with 1.6.0_24 I do not get the message.
It also depends on where the String String b2 =...
is in the source code. The following code also does not display a message:
class Test { public static void main(String... args) { String s1 = "Good"; s1 = s1 + "morning"; String s2 = "Goodmorning"; System.out.println(s1.intern());
it seems that intern
after not finding a row in the row pool, inserts the actual instance of s1 into the pool. The JVM uses this pool when creating s2, so it gets the same link as s1. On the other hand, if s2 is created first, this link is stored in the pool.
This may be the result of moving interned strings from the ongoing generation of the Java heap.
Found here: Important RFEs Addressed in JDK 7
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but instead are allocated in the main part of the Java heap (the so-called young and old generations) along with other created objects on request. This change will result in more data residing in the main Java heap and less data in the permanent generation, and therefore heap size adjustments may be required. Because of this change, most applications will only see relatively small differences in heap usage, but larger applications that load many classes or use the String.intern () method intensively will see more significant differences.
Not sure if this is an error and from which version ... JLS 3.10.5 Status
The result of explicitly interning the calculated string is the same string as any pre-existing literal string with the same contents.
so the question is how are pre-existing ones interpreted, compilation time or runtime: is Goodmorning already existing or not?
I prefer how it was implemented before 7 ...
Carlos Heuberger Aug 17 2018-11-11T00: 00Z
source share