一般會答: 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[];