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

一般会答: s.length()

看源码是如何实现的:

    /**
     * Returns the length of this string.
     * The length is equal to the number of <a href="Character.html#unicode">Unicode
     * code units</a> 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[];
posted @ 2018-12-01 08:26 小鸣的微笔记 阅读(...) 评论(...) 编辑 收藏