代码之家  ›  专栏  ›  技术社区  ›  Aillyn

Java字符串中的底层容器是什么?

  •  6
  • Aillyn  · 技术社区  · 15 年前

    它只是一个字符数组吗?

    1 回复  |  直到 15 年前
        1
  •  10
  •   Thilo    15 年前

    是的,加上一些元数据,比如开始索引和结束索引(因为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