Java中length和length()的區別

length是數組的屬性

length()是字符串類的方法,本質上也是調用了數組的length屬性。因爲字符串底層也是char數組嘛!

    /**
     * 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也是String類的一個char類型的數組。String的底層也就是一個char類型的數組。

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

 用法區別:

 

 

 所以這兩個玩意本質上並沒有區別。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章