String中的intern试题解析

来源:互联网 发布:淘宝人群标签 编辑:程序博客网 时间:2024/06/07 00:09

今天发现自己对String中的intern这个方法是我的一个盲点;对其具体的学习以后,对这些有一些理解。


String s1="123";// 常量池  
String s2=new String("123");//堆  
s2.intern();// 拷贝时发现已存在,没必要拷贝,实际无任何影响  
System.out.println(s1==s2);//false



//String s1="123";  
String s2=new String("123"); // 记得这个题吗?新建多少个对象?答案两个:常量池1个,堆上一个  
// 注意常量池其实有了123  
s2.intern();// 没有任何影响  
System.out.println("123"==s2);//false 



String s2=new String("123")+new String( "456" );  
s2.intern(); // 常量池没有123456,  
// [1.6]拷贝对象到常量池,返回新引用,但s2不变还是指向堆,s2.intern()!=s2  
// [1.7]常量池中不需要再存储一份对象了  
//拷贝引用而不是拷贝对象,也就是说s2.intern() ==s2会返回true。  
//注意前提是常量池没有这个串(或其引用),如果已经有了不会发生任何拷贝,只会返回池中引用。  
String s1="123456";// 使用常量池已有,得到引用  
System.out.println(s1==s2);// [1.6]false [1.7]true




【1.7】调用intern方法,会将字符串对象中值复制到常量池中,若常量池中已经存在该字符串,所以直接返回该字符串的引用


intern   JDK中源码 以及 注释


/** * Returns a canonical representation for the string object. * <p> * A pool of strings, initially empty, is maintained privately by the * class {@code String}. * <p> * When the intern method is invoked, if the pool already contains a * string equal to this {@code String} object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this {@code String} object is added to the * pool and a reference to this {@code String} object is returned. * <p> * It follows that for any two strings {@code s} and {@code t}, * {@code s.intern() == t.intern()} is {@code true} * if and only if {@code s.equals(t)} is {@code true}. * <p> * All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * <cite>The Java&trade; Language Specification</cite>. * * @return  a string that has the same contents as this string, but is *          guaranteed to be from a pool of unique strings. */