2010-09-15 29 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