深入理解java虛擬機(三):String.intern()-字符串常量池

看源碼:

 public native String intern();
 
 Returns a canonical representation for the string object.

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 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.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns:
a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

 

意思是說當調用 intern 方法時,如果池已經包含一個等於此 String 對象的字符串(該對象由 equals(Object) 方法確定),則返回池中的字符串。否則,將此 String 對象添加到池中,並且返回此 String 對象的引用

看下面這段代碼:

[java] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. public class StringInternTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         String str1 = new StringBuilder("chaofan").append("wei").toString();  
  6.         System.out.println(str1.intern() == str1);  
  7.   
  8.         String str2 = new StringBuilder("ja").append("va").toString();  
  9.         System.out.println(str2.intern() == str2);  
  10.     }  
  11. }  


有圖可知,當在jdk1.6中運行時,會得到兩個false,而在jdk1.7中運行,會得到一個true和一個false。
產生的差異在於在jdk1.6中 intern 方法會把首次遇到的字符串實例複製到永久待(常量池)中,並返回此引用;但在jdk1.7中,只是會把首次遇到的字符串實例的引用添加到常量池中(沒有複製),並返回此引用
所以在jdk1.7中執行上面代碼,str1返回true是引用他們指向的都是str1對象(堆中)(池中不存在,返回原引用),而str2返回false是因爲池中已經存在"java"了(關鍵詞),所以返回的池的對象,因此不相等。

 

看下面實例:

[java] view plain copy
 print?在CODE上查看代碼片派生到我的代碼片
  1. @Test  
  2.     public void test3(){  
  3.           
  4.         String str1 = "a";  
  5.         String str2 = "b";  
  6.         String str3 = "ab";  
  7.         String str4 = str1 + str2;  
  8.         String str5 = new String("ab");  
  9.            
  10.         System.out.println(str5.equals(str3));//true  
  11.         System.out.println(str5 == str3);//false  
  12.         System.out.println(str5.intern() == str3);//true  
  13.         System.out.println(str5.intern() == str4);//false  
  14.           
  15.           
  16.     }  
  17.     @Test  
  18.     public void test4(){  
  19.           
  20.         String a = new String("ab");  
  21.         String b = new String("ab");  
  22.         String c = "ab";  
  23.         String d = "a" + "b";  
  24.         String e = "b";  
  25.         String f = "a" + e;  
  26.   
  27.         System.out.println(b.intern() == a);//fasle  
  28.         System.out.println(b.intern() == c);//true  
  29.         System.out.println(b.intern() == d);//true  編譯期d已確定(修改、賦值)爲ab  
  30.         System.out.println(b.intern() == f);//false  
  31.         System.out.println(b.intern() == a.intern());//true  
  32.           
  33.           
  34.     }  
  35.     @Test  
  36.     public void test5(){  
  37.         // 編譯期已確定  
  38.         String a = "abc";  
  39.         String b = "abc";  
  40.         String c = "a" + "b" + "c";  
  41.         String d = "a" + "bc";  
  42.         String e = "ab" + "c";  
  43.                   
  44.         System.out.println(a == b);//true   
  45.         System.out.println(a == c);//true  
  46.         System.out.println(a == d);//true  
  47.         System.out.println(a == e);//true  
  48.         System.out.println(c == d);//true  
  49.         System.out.println(c == e);//true  
  50.           
  51.           
  52.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章