字符串变量相加vs字符串常量相加
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3==s1+s2);
System.out.println(s3=="hello"+"world");
}
原因:字符串变量相加时,先开辟空间再相加;
字符串常量相加时,先相加,然后在字符串常量池找,如果有就直接返回,否则就创建。