String str = "a";
int[] array = new int[10];
System.out.println(
"文字列の長さ = " + str.length()
+ "配列の要素数 = " + array.length);
なぜ、配列のlength には()
がつかないのか?
疑問に思ってAPI のソース(1.8)を見てみました。
String.java#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 は、charの配列ですね。
/** The value is used for character storage. */
private final char value[];
配列のlength
一般的には配列オブジェクトのフィールド変数と説明されるやつですね。
が、あれ、クラスはどれみればいいんだ?
java.lang.reflect.Array にもそんなフィールドはないし。
java.lang.Class の中にこんなのがあったけど
2699 static class MethodArray {
2700 private Method[] methods;
2701 private int length;
MethodArray?
どなたか識者のかたお助けを..