class Test {
public static void main(String...args) {
String s1 = \"Good\";
s1 = s1 + \"morning\";
System.out.println(s1.intern());
The result code dependents runtime:
class Test {
public static void main(String... args) {
String s1 = "Good";
s1 = s1 + "morning";
System.out.println(s1 == s1.intern()); // Prints true for jdk7, false - for jdk6.
}
}
If you write like this:
class Test {
public static void main(String... args) {
String s = "GoodMorning";
String s1 = "Good";
s1 = s1 + "morning";
System.out.println(s1 == s1.intern()); // Prints false for both jdk7 and jdk6.
}
}
the reason is ' ldc #N ' (Load string from constant pool) and String.intern() both will use StringTable in hotspot JVM. For detail I wrote a pool english article: http://aprilsoft.cn/blog/post/307.html