What is the underlying container in a Java String?
I开发者_JAVA百科s it just a char array?
Yes, plus some meta-data, like a start and end index (because that char array can be shared across strings, for example, when you create substrings).
Looking at the source for java.lang.String
, you see the following instance fields:
/** 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
精彩评论