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 國際許可協議發佈,歡迎轉載,演繹或用於商業目的,可是必須保留本文的署名及連接。