StringTable面试题
作者:互联网
public static void main(String[] args){
String s1 = "a";
String s2 = "b";
String s3 = "a" + "b";//编译期优化为”ab“,常量池没有,入池
String s4 = s1 + s2;//new String(”ab“)堆中的对象
String s5 = "ab";//常量池中已有
String s6 = s4.intern();//池中已有,直接返回常量池中的
//问
System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//ture
System.out.println(s3 == s6);//ture
String x2 = new String("c") + new String("d");//堆
String x1 = "cd";//常量池
x2.intern();//入池失败
//问,如果调换了位置呢,如果是jdk1.6呢
System.out.println(x1 == x2);//false
}
标签:面试题,String,常量,s3,StringTable,System,println,out 来源: https://blog.csdn.net/weixin_43783251/article/details/118893111