dot length in java - finding its definition

前端 未结 7 1187
野趣味
野趣味 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).

    0 讨论(0)
  • 2021-01-03 00:48

    Array types are special in Java. This is an excerpt from JLS 10.7 Array Members

    The members of an array type are all of the following:

    • The public final field length, which contains the number of components of the array (length may be positive or zero).
    • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
    • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

    Despite this, there are still old-standing bugs when you're using reflection on arrays: neither length nor clone could be found through reflection (bug# 5047859, bug# 4987375).

    Neither member is inherited in the traditional way from any superclass; all array types extends from Object directly. This "special treatment" is likely why these bugs exist in the first place.


    "does this mean that never will one be able to see the length variable being defined?"

    There is no actual source code for array types. Again, these types are special; the JVM just pulls out these types out of a hat whenever they're required. You will not see a .java source file for the array type int[].class (i.e. the type of all int array).

    So, no, you will not be able to see the length field defined.


    For further reading, here are some information on Java Reflection:

    • The Java Tutorials: Reflection API
    • Advanced Language Topic technical article: Using Java Reflection
    0 讨论(0)
  • 2021-01-03 00:53

    In java with arrays you can do this. For example:

    String[] a = new String [] {"a","b","c"};
    int length = a.length;
    

    Length would be 3;

    0 讨论(0)
  • 2021-01-03 00:58

    Its a variable that can be accessed for an array to find out how many elements there are in the array.

    0 讨论(0)
  • 2021-01-03 01:04

    Arrays are not like regular classes, they are a special case defined in the Java Language Specification. So the .length public final variable is defined in the Java Language Specification, but it is not actually defined anywhere in a .java or a .class file.

    0 讨论(0)
  • 2021-01-03 01:04

    You need to be a bit more specific than that.

    A variable length can exist in any class. If you mean from the API then you are probably looking for the Array.

    Some classes have a length() method.

    0 讨论(0)
提交回复
热议问题