JAVA字符串池(String pool)的困惑题目
废话不多说,先放图。
在Java中有两种创建字符串对象的方式:
1)采用字面值的方式赋值
2)采用new关键字新建一个字符串对象。这两种方式在性能和内存占用方面存在着差别。
public class Test {
/*字符串一旦创建,不能发生改变长度。
*/
public static void main(String[] args) {
String s1 = “Hello World” ;
//在编译时运行
String s2 = "Hello " + “World” ;
String temp1 = “Hello " ;
String temp2 = “World” ;
//两个变量相加在运行时相加运算
String s5 = temp1 + temp2 ;
String s6=(temp1+temp2).intern();
//符号”"是比较内存地址
boolean flag1 = s1 s2 ;
boolean flag2 = s1s5 ;
boolean flag3=s1s6;
System.out.println(“flag1:”+flag1);
System.out.println(“flag2:”+flag2);
System.out.println(“flag3:”+flag3);
}
}
详情参考https://www.cnblogs.com/cold-windy/p/11514977.html