String是Java基础的重要考点。可问的点多,并且不少点能够横向切到其余考点,或纵向深刻JVM。html
本文略过了String的基本内容,重点在于String#intern()。前端
String常量可能会在两种时机进入常量池:java
时机1的行为是明确的。原理可阅读class文件结构、类加载、编译期即运行期优化等内容。c++
时机2在jdk6和jdk7中的行为不一样,下面讨论。git
读者可直接阅读参考资料。下述总结仅为了猴子本身复习方便。程序员
/** * Returns a canonical representation for the string object. * <p> * A pool of strings, initially empty, is maintained privately by the * class <code>String</code>. * <p> * When the intern method is invoked, if the pool already contains a * string equal to this <code>String</code> object as determined by * the {@link #equals(Object)} method, then the string from the pool is * returned. Otherwise, this <code>String</code> object is added to the * pool and a reference to this <code>String</code> object is returned. * <p> * It follows that for any two strings <code>s</code> and <code>t</code>, * <code>s.intern() == t.intern()</code> is <code>true</code> * if and only if <code>s.equals(t)</code> is <code>true</code>. * <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™ 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. */
public native String intern();
复制代码
String#intern()是一个native方法。根据Javadoc,若是常量池中存在当前字符串, 就会直接返回当前字符串. 若是常量池中没有此字符串, 会将此字符串放入常量池中后, 再返回。github
JNI最后调用了c++实现的StringTable::intern()方法:express
oop StringTable::intern(Handle string_or_null, jchar* name,
int len, TRAPS) {
unsigned int hashValue = java_lang_String::hash_string(name, len);
int index = the_table()->hash_to_index(hashValue);
oop string = the_table()->lookup(index, name, len, hashValue);
// Found
if (string != NULL) return string;
// Otherwise, add to symbol to table
return the_table()->basic_add(index, string_or_null, name, len,
hashValue, CHECK_NULL);
}
oop StringTable::lookup(int index, jchar* name,
int len, unsigned int hash) {
for (HashtableEntry<oop>* l = bucket(index); l != NULL; l = l->next()) {
if (l->hash() == hash) {
if (java_lang_String::equals(l->literal(), name, len)) {
return l->literal();
}
}
}
return NULL;
}
复制代码
在the_table()返回的hash表中查找字符串,若是存在就返回,不然加入表。bash
StringTable是一个固定大小的Hashtable,默认大小是1009。基本逻辑与Java中HashMap相同,也使用拉链法解决碰撞问题。oop
既然是拉链法,那么若是放进的String很是多,就会加重碰撞,致使链表很是长。最坏状况下,String#intern()的性能由O(1)退化到O(n)。
- jdk6中StringTable的长度固定为1009。
- jdk7中,StringTable的长度能够经过一个参数
-XX:StringTableSize
指定,默认1009。
相信不少Java程序员都作相似String s = new String("abc");
这个语句建立了几个对象的题目。这种题目主要是为了考察程序员对字符串对象常量池的掌握。上述的语句中建立了2个对象:
来看一段代码:
public static void main(String[] args) {
String s = new String("1");
s.intern();
String s2 = "1";
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);
}
复制代码
打印结果:
# jdk6下
false false
# jdk7下
false true
复制代码
具体为何稍后再解释,而后将s3.intern();
语句下调一行,放到String s4 = "11";
后面。将s.intern();
放到String s2 = "1";
后面:
public static void main(String[] args) {
String s = new String("1");
String s2 = "1";
s.intern();
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
String s4 = "11";
s3.intern();
System.out.println(s3 == s4);
}
复制代码
打印结果:
# jdk6下
false false
# jdk7下
false false
复制代码
注:图中绿色线条表明String对象的内容指向;黑色线条表明地址指向。
jdk6中,上述的全部打印都是false。
由于jdk6的常量池放在Perm区中,和正常的Heap(指Eden、Surviver、Old区)彻底分开。具体来讲:使用引号声明的字符串都是经过编译和类加载直接载入常量池,位于Perm区;new出来的String对象位于Heap(E、S、O)中。拿一个Perm区的对象地址和Heap中的对象地址进行比较,确定是不相同的。
Perm区主要存储一些加载类的信息、静态变量、方法片断、常量池等。
在jdk6及以前的版本中,字符串常量池都是放在Perm区的。Perm区的默认大小只有4M,若是多放一些大字符串,很容易抛出OutOfMemoryError: PermGen space
。
所以,jdk7已经将字符串常量池从Perm区移到正常的Heap(E、S、O)中了。
Perm区即永久代。自己用永久代实现方法区就容易遇到内存溢出;并且方法区存放的内容也很难估计大小,不必放在堆中管理。jdk8已经取消了永久代,在堆外新建了一个Metaspace实现方法区。
正是由于字符串常量池移到了Heap中,才产生了上述变化。
先看s3和s4:
String s3 = new String("1") + new String("1");
,生成了多个对象,s3最终指向堆中的"11"。注意,此时常量池中是没有字符串"11"的。s3.intern();
,将s3中的字符串"11"放入了常量池中,由于此时常量池中不存在字符串"11",所以常规作法与跟jdk6相同,在常量池中生成一个String对象"11"——然而,jdk7中常量池不在Perm区中了,相应作了调整:常量池中不须要再存储一份对象了,而是直接存储堆中的引用,也就是s3的引用地址。String s4 = "11";
,"11"经过双引号显示声明,所以会直接去常量池中查找,若是没有再建立。发现已经有这个字符串了,也就是刚才经过s3.intern();
存储在常量池中的s3的引用地址。因而,直接返回s3的引用地址,s4赋值为s3的引用,s4指向堆中的"11"。s3 == s4
。再看s和s2:
String s = new String("1");
,生成了2个对象,常量池中的"1"和堆中的"1",s指向堆中的"1"。s.intern();
,上一句已经在常量池中建立了"1",因此此处什么都不作。String s2 = "1";
,常量池中有"1",所以,s2直接指向常量池中的"1"。s == s2
。先看s3和s4,将s3.intern();
放在了String s4 = "11";
后:
String s4 = "11";
,此时,常量池中不存在"11",所以,将"11"放入常量池,而后s4指向常量池中的"11"。s3.intern();
,上一句已经在常量池中建立了"11",因此此处什么都不作。s3 == s4
。再看s和s2,将s.intern();
放到String s2 = "1";
后:
String s2 = "1";
,以前已经过String s = new String("1");
在常量池中建立了"1",所以,s2直接指向常量池中的"1"。s.intern();
,常量池中有"1",因此此处什么都不作。s == s2
。jdk7与jdk6相比,对String常量池的位置、String#intern()的语义都作了修改:
建议直接阅读参考资料。
String#intern()的基本用法以下:
String s1 = xxx1.toString().intern();
String s2 = xxx2.toString().intern();
assert s1 == s2;
复制代码
然而,xxx1.toString()
、xxx2.toString()
已经建立了两个匿名String对象,这以后再调用String#intern()。那么,这两个匿名对象去哪了?
估计猴子对建立对象的过程理解有问题,或许
xxx1.toString()
返回时尚未将对象保存到堆上?或许String#intern()上作了什么语法糖?后面有时间再解决吧。。。
参考:
本文连接:
本文连接:String常量池和String#intern()
做者:猴子007
出处:monkeysayhi.github.io
本文基于 知识共享署名-相同方式共享 4.0 国际许可协议发布,欢迎转载,演绎或用于商业目的,可是必须保留本文的署名及连接。