与字符串实习相关
2022-09-02 20:36:02
public static void main(String[] args) {
String a = new String("lo").intern();
final String d = a.intern();
String b = "lo";
final String e = "lo";
String c = "Hello";
System.out.println(b==a);//true
System.out.println(d==a);//true
System.out.println(e==a);//true
System.out.println(c=="Hel"+a); //why is this false? when e==a is true
System.out.println(c=="Hel"+d); //why is this false?
System.out.println(c=="Hel"+b); //why is this false?
System.out.println(c=="Hel"+e); //this is true
}
这导致
true
true
true
false
false
false
true
表达式为 true 意味着相同的引用。那么为什么最后一个表达式是真的,而倒数第4个即是假的呢?e==a
c== "Hel"+a