基于前一篇博客,现在来看这样一段代码:
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello" + "world";
String s5 = s1 + "world";
String s6 = "hello" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
根据上一篇笔记,在执行s4 = "hello" + "world"之前,字符串常量池中已有"hello""world""helloworld"这些字符串。
s3 == s4:s4 = "hello" + "