Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is array's length property defined?

Tags:

java

arrays

People also ask

What is the array length property?

The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Is array length a method or property?

length is a property of array, not a method if it was a method getting the length of an array would be of O(n), but by keeping it as a property its O(1).

Where is the length of an array stored Java?

As mentioned earlier, the JVM stores the array length inside the object header after mark and klass words. Also, the array length will be stored in 4 bytes, so it can't be greater than the maximum value for a 32-bit integer. After the object header, the JVM stores the actual array elements.

What is the purpose of length property in array?

The length property sets or returns the number of elements in an array.


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:

  • JLS - Arrays

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 :(


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.


Even though this is not a direct answer to the question, it is an addition to the .length vs .size() argument. I was researching something related to this question so when I came across it I noticed that the definition(s) provided here

The public final field length, which contains the number of components of the array.

is not "exactly" correct.

The field length contains the number of available places to put a component, not the number of components present in the array. So it represents the total available memory allocated to that array, not how much of that memory is filled.

Array Memory Allocation

Example:

static class StuffClass {
    int stuff;
    StuffClass(int stuff) {
        this.stuff = stuff;
    }
}

public static void main(String[] args) {

    int[] test = new int[5];
    test[0] = 2;
    test[1] = 33;
    System.out.println("Length of int[]:\t" + test.length);

    String[] test2 = new String[5];
    test2[0] = "2";
    test2[1] = "33";    
    System.out.println("Length of String[]:\t" + test2.length);

    StuffClass[] test3 = new StuffClass[5];
    test3[0] = new StuffClass(2);
    test3[1] = new StuffClass(33);
    System.out.println("Length of StuffClass[]:\t" + test3.length);         
}

Output:

Length of int[]:        5
Length of String[]:     5
Length of StuffClass[]: 5

However, the .size() property of the ArrayList does give the number of elements in the list:

ArrayList<Integer> intsList = new ArrayList<Integer>();
System.out.println("List size:\t" + intsList.size());
intsList.add(2);
System.out.println("List size:\t" + intsList.size());
intsList.add(33);
System.out.println("List size:\t" + intsList.size());

Output:

List size:  0
List size:  1
List size:  2