String a ="zyzx";
String b ="zyzx";
String c ="zy" +"zx";
String d =new String("zyzx");
String e ="zy";
String f ="zx";
String g = e + f;
String h = e +"zx";
final String i ="zx";
String j ="zy" + i;
String result ="a == b:" + (a == b) +"\n"
+"a == c:" + (a == c) +"\n"
+"a == d:" + (a == d) +"\n"
+"a == (e + f):" + (a == (e + f)) +"\n"
+"a == g:" + (a == g) +"\n"
+"a == h:" + (a == h) +"\n"
+"a == j:" + (a == j) +"\n";
结果:
jdk 1.6以前常量池在方法区中,jdk 1.7后移动到堆中
new出来的字符串创建在堆中,直接引号的字符串创建在常量池中
字符串相加过程中有变量的话,会先在常量池创建,然后拼接地址
字符串相加过程中都为常量的话,会先拼接字符串,再去常量池找