Where is array's length property defined?

2018-12-31 16:12发布

We can determine the length of an ArrayList<E> using its public method size(), like

ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();

Similarly we can determine the length of an Array object using the length property

String[] str = new String[10];
int size =  str.length;

Whereas the size() method of ArrayList is defined inside the ArrayList class, where is this length property of Array defined?

标签: java arrays
7条回答
只靠听说
2楼-- · 2018-12-31 16:50

it's public final field , which contains the number of components of the array (length may be positive or zero)

An array thus has the same public fields and methods as the following class:

class A implements Cloneable, java.io.Serializable {
    public final int length = X;
    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new InternalError(e.getMessage());
        }
    }
}

more info at

10.7 Array Members

http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

查看更多
只若初见
3楼-- · 2018-12-31 16:55

It's defined in the Java language specification:

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.

Since there is a limitless number of array types (for every class there is a corresponding array type, and then there are multidimensional arrays), they cannot be implemented in a class file; the JVM has to do it on the fly.

查看更多
回忆,回不去的记忆
4楼-- · 2018-12-31 16:57

To answer it as it-is, where is this length property of array defined? In a special Object header.

Easy to see via JOL

 int [] ints = new int[23];
 System.out.println(ClassLayout.parseInstance(ints).toPrintable());

One of the lines from this output is going to be:

OFFSET  SIZE      TYPE DESCRIPTION
16       4        (object header)   17 00 00 00 (00010111 00000000 00000000 00000000) (23)

Usually Objects have two headers (mark and klass), arrays have one more that always occupy 4 bytes in length, as size is an int.

查看更多
不流泪的眼
5楼-- · 2018-12-31 17:00

The keyword length acts like a data filed defined. When using in an array, we can use it to access how many elements in an array. Regarding to String[], we can invoke length() method defined in String class. With regard to ArrayList, we can use size() method defined in ArrayList. Note that when creating an array list with ArrayList<>(capacity), the initial size() of this array list is zero since there is no element.

查看更多
大哥的爱人
6楼-- · 2018-12-31 17:04

It's "special" basically, with its own bytecode instruction: arraylength. So this method:

public static void main(String[] args) {
    int x = args.length;
}

is compiled into bytecode like this:

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   arraylength
   2:   istore_1
   3:   return

So it's not accessed as if it were a normal field. Indeed, if you try to get it as if it were a normal field, like this, it fails:

// Fails...
Field field = args.getClass().getField("length");
System.out.println(field.get(args));

So unfortunately, the JLS description of each array type having a public final field length is somewhat misleading :(

查看更多
初与友歌
7楼-- · 2018-12-31 17:10

Arrays are special objects in java, they have a simple attribute named length which is final.

There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself.

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[].

    A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

Resources:

查看更多
登录 后发表回答