dot length in java - finding its definition

前端 未结 7 1246
野趣味
野趣味 2021-01-03 00:31

In which class is the length field defined in Java (e.g. for array length)? Will I be able to see it defined say in Object class?

EDIT : Wh

7条回答
  •  忘掉有多难
    2021-01-03 00:46

    Any class can have a field called length. Just add it.

    Arrays do not have a length field; they have something which looks like such a field:

    int[] a = new int[10];
    System.out.println("array length is: " + a.length);
    

    but it is not really a plain field, because this does not compile:

    int[] a = new int[10];
    a.length = 42;  // <- here the compiler gets grumpy
    

    and, at the JVM level, the array length is accessed with a specific opcode (arraylength) and not the generic instance field access opcode (getfield).

提交回复
热议问题