public class StringDemo{ public static void main(String[] args){
String s1 = "a";
String s2 = "b";
String s3 = "ab";
System.out.println(s3 == s1 + s2); //输出false
System.out.println(s3 == "a" + "b"); //输出true
}
}
解释:变量相加会在堆内存重新开辟空间,所以s1+s2的地址是指向堆内存的,
而s3的地址则是指向方法区的字符串常量池,所以不会相等
|
|