以下讨论以jdk8为标准:
-  
String Pool:字符串常量池
- 存储字面量
 - 位于堆中,不在元空间
 - intern()方法会去常量池找,没有的话就创建一个,返回常量池中的地址;有的话就直接返回对象地址
 
 -  
new String(“”)方法强制创建一个新的String对象,并且不会调用inter(),也就是说即使常量池里面没有,也不会添加进去。
 -  
对下面代码逐行解释 (里面world和hello的垃圾回收暂时不考虑)
String str = new String("hello") + new String("world"); str.intern(); String str1 = "helloworld"; System.out.println(str==str1); // true-  
String str = new String("hello") + new String("world"); 
 -  
 

 2. java str.intern(); 
 
 3. java String str1 = "helloworld"; 
 
 4.
 String str = new String("hello") + new String("world");
      
      String str1 = "helloworld";
      str.intern();
      System.out.println(str==str1);		//	false
 
只解释为什么是false,因为没有对str = str.intern()进行赋值
 
String str = new String("hello") + new String("world");
String str1 = "helloworld";
str = str.intern();
System.out.println(str==str1);		//	true
                


















