Just run the below codes in jdk1.8(mine is jdk1.8.0_60), the result is:
true
false
true,
The method c2()
returns false
, why?
I just want to know about the runtime constant pool. Not about comparing different strings.
Attempt
public static void main(String[] args) throws InterruptedException {
c1();
c2();
c3();
}
private static void c1() {
String s1 = new String("a") + new String("b");
s1.intern();
String s2 = "ab";
System.out.println(s1 == s2);
}
private static void c2() {
String s1 = new String("h") + new String("e");
s1.intern();
String s2 = "he";
System.out.println(s1 == s2);
}
private static void c3() {
String s1 = new String("e") + new String("h");
s1.intern();
String s2 = "eh";
System.out.println(s1 == s2);
}