A pool of strings, initially empty, is maintained privately by the
class String.
When the intern method is invoked, if the pool already contains a
string equal to this String object as determined by
the {@link #equals(Object)} method, then the string from the pool is
returned. Otherwise, this String object is added to the
pool and a reference to this String object is returned.
It follows that for any two strings s and t,
s.intern() == t.intern() is true
if and only if s.equals(t) is true.
"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. the pool and a reference to this String object is returned. "
每個類別中的常數池:
要注意的是:
至於String.intern()方法,引用其JavaDoc:
加粗標明的行也說明了java在方法區中會維持string pool。至於為什麼
相信你看了javaDoc會明白。
PS:不光String類別有JVM原生支援的常數池,Boolean、Byte、Character、Short、Integer、Long這六種原始類型包裝類,也實作了程式碼層級的常數池,具體可參考JDK源碼。
PPS:可供參考常數池
Java Doc:
"When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. the pool and a reference to this String object is returned. "
是說, 在呼叫str.intern()時, 首先去pool中找是否有equals(str)==true的字串. 找到後返回已存在於pool裡的字串, 否則把str加入pool中.
所以在pool裡, 相等的字串 只會有一份.
可能我問題有點問題~,「運行時常數池是方法去的一部分,Class檔案中除了有類別的版本、欄位、方法、介面等描述資訊外,還有一項資訊就是常數池,用於存放編譯器產生的各種字面量和符號引用,這部分內容在類別載入後存放在方法區的運行時常數池中。時常量池的一個引用
如果字串StringA和StringB有著相同的內容,那麼上面的語句
StringA.intern()==StringB.intern()
為true,因為他們指向的為同一個物件。呼叫intern後,首先檢查字串常數池中是否有該物件的引用,如果存在,則將這個引用傳回給變量,否則將引用加入並傳回給變數。
具體可以參考Java中的字串常數池