关于java字符串常量池
今天发现一个好玩的东西
public static void main(String[] args)
{
String str1 = new StringBuilder("ja").append("va").toString();
System.out.println(str1.intern() == str1);//false
String str2 = new StringBuilder("in").append("t").toString();
System.out.println(str2.intern() == str2);//false
String str3 = new StringBuilder("in").append("tu").toString();
System.out.println(str3.intern() == str3);//true
String str4 = new StringBuilder("in").append("tu").toString();
System.out.println(str4.intern() == str4);//false
String str7 = new StringBuilder("tao").toString();
System.out.println(str7.intern() == str7);//false
String str5 = "caotao";
System.out.println(str5.intern() == str5);//true
String str6 = new String("xiao");
System.out.println(str6.intern() == str6);//false
}
我想应该是初始化时java把java这个字符串,void字符串以及int,byte,char,double,float,boolean,short,long这八个字符串都放在了字符串常量池中。。。
各位大佬,小弟猜的对不对?