//字符串常量池
String str1 = "str";
String str2 = "ing";
String str3 = "str" + "ing";
String str4 = str1 + str2;
System.out.println(str3 == str4); //false
String str5 = "string";
System.out.println(str3 == str5); //true
// 将两个常量用+连接对s进行初始化
String s = A + B; // 将两个常量用+连接对s进行初始化 等同于:String s="ab"+"cd";
String t = "abcd";
System.out.println(s == t); //true
String _s = _A + _B;
String _t = "abcd";
System.out.println(_s == _t); //false
//String.intern
String s1 = new String("计算机");
String s2 = s1.intern();
String s3 = "计算机";
System.out.println("s1 == s2? " + (s1 == s2)); //false
System.out.println("s3 == s2? " + (s3 == s2)); //true
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
String _hello = "Hel"+lo;
System.out.print((hello == _hello) + " ");
System.out.println(hello == _hello.intern()); //true true false true
//jdk1.7以后字符串常量池不在永久代中,而在堆中,String.intern会不会拷贝字符串到常量池中,
//而是在常量池中保存指向堆中intern对象的引用
String str11 = new String("hello");
str11 = str11 + "world";
str11.intern();
String str12 = "helloworld";
System.out.println(str11 == str12); //true
String str21 = new String("how");
str21 = str21 + "areyou";
String str22 = "howareyou";
str21.intern();
System.out.println(str21 == str22); //false