[源码] 定义String s="abcd", 求长度


一般会答: s.length()

看源码是如何实现的:

    /**
     * Returns the length of this string.
     * The length is equal to the number of Unicode
     * code units in the string.
     *
     * @return  the length of the sequence of characters represented by this
     *          object.
     */
    public int length() {
        return value.length;
    }

value就是一个字符数组, 即s.length()其实是调用char[]的length属性

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