2010-09-15 33 views
6

它只是一個字符數組?Java字符串中的底層容器是什麼?

+0

可能是一個int [] – irreputable 2010-09-15 05:13:37

+0

+1你的好奇心...查看:http://www.docjar.com /html/api/java/lang/String.java.html – 2010-09-15 05:19:27

+0

@irreputable ...但事實並非如此。 – 2010-09-15 06:33:56

回答

10

是的,加上一些元數據,如開始和結束索引(因爲可以跨字符串共享字符數組,例如,當您創建子字符串時)。

縱觀源java.lang.String,你看到下面的實例字段:

/** The value is used for character storage. */ 
private final char value[]; 

/** The offset is the first index of the storage that is used. */ 
private final int offset; 

/** The count is the number of characters in the String. */ 
private final int count; 

/** Cache the hash code for the string */ 
private int hash; // Default to 0 
+0

和一些方法和算法...作爲'字符串'是一個對象,而不是一個原始'數據類型' – 2010-09-15 05:19:00

+1

@Garis是的,但這不是容器,這是頂部的糖 – 2010-09-15 06:53:32