Java String中的底层容器是什么?

时间:2010-09-15 05:08:13

标签: java string

它只是一个char数组吗?

1 个答案:

答案 0 :(得分:10)

是的,加上一些元数据,比如开始和结束索引(因为可以跨字符串共享char数组,例如,在创建子字符串时)。

查看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