public class demo01 {
public static void main(String[] args) {
String s1 = "hellojava";
String s2 = "hello";
String s3 = "java";
String s4 = "hello" + s3;
String s5 = s2 + "java";
String s6 = s2 + s3;
String s7 = "hello" + "java";
String s8 = new String("helloworld");
System.out.println(s4);
System.out.println(s1 == s4);
System.out.println(s5);
System.out.println(s1 == s5);
System.out.println(s6);
System.out.println(s1 == s6);
System.out.println(s7);
System.out.println(s1 == s7);
System.out.println(s8);
System.out.println(s1 == s8);
}
}
输出结果:
hellojava
false
hellojava
false
hellojava
false
hellojava
true
helloworld
false
false
为什么结果会不同?![]
总结:所以来说 字符串+字符串不一定等于字符串
字符串常量 + 字符串常量 = 字符串常量
原文:https://www.cnblogs.com/tyhA-nobody/p/12897729.html