46

I was wondering about the implementation of length of a Java Array. I know that using arrayName.length gives us the number of elements in the array, but was wondering if this is a method / function or it is just a data member of Array?

I guess it must be a data member as we do not use parenthesis() when invoking it. But if it is a data member how/when is the value of this length assigned/computed?

2
  • Anywhere? Not even in the JLS? It's syntactically a member variable of the array object, though one you can't assign to. Commented May 10, 2011 at 12:44
  • JLS.. JLS is the answer. Commented Dec 14, 2017 at 6:32

9 Answers 9

45

According to the Java Language Specification (specifically §10.7 Array Members) it is a field:

  • The public final field length, which contains the number of components of the array (length may be positive or zero).

Internally the value is probably stored somewhere in the object header, but that is an implementation detail and depends on the concrete JVM implementation.

The HotSpot VM (the one in the popular Oracle (formerly Sun) JRE/JDK) stores the size in the object-header:

[...] arrays have a third header field, for the array size.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks folks. With your helpful replies (and a little reading up), I found that "final fields" can be set in a place other declaration (I had earlier erroneously assumed they could only be defined at the same time they were declared). So I guess the ctor code of the Array class computes the number of elements and assigns this value to 'length'.
great work! the white paper said two-word object header for plain object, but I get the header information by Java-Object-Layout tool with 3 words object header. What's wrong?
Okay, it says in the design document but I check Java source code, I don't see the implementation? Why?
@WeijingJayLin: arrays can not be implemented in Java, as they would require ... arrays, basically. They are in the JDK code, of course, but not in the Java portion of it. What source files where you expecting that field to be in?
@JoachimSauer Okay, I guess it on Java Engine side, in c/c++?
18

You're correct, length is a data member, not a method.

From the Arrays tutorial:

The length of an array is established when the array is created. After creation, its length is fixed.

10 Comments

technically it's not even a field, since the JVM instruction is not 'GETFIELD' (but ARRAYLENGTH), in the same aspect the class can be considered data member (also stored in the object header, or even part of the pointer)
@bestsss: That may be technically correct, but I don't know if JVM instructions are the right level of abstraction to be answering this question. The JLS calls length both a member and a field. java.sun.com/docs/books/jls/third_edition/html/…
From the language perspective it is clearly a field: it behaves exactly the same as all other public final fields.
@Bill, try using reflection to obtain the field (as java.lang.reflect.Field), it won't work either. That makes it practically inaccessible via Unsafe as well. From java source point of view there is no difference between any public final int field and the array length but anything below that level the two are quite different.
@bestsss: you're absolutely right. But most people (especially beginners) don't care about anything below the language level ;-)
|
4

If you have an array of a known type or is a subclass of Object[] you can cast the array first.

Object array = new ????[n];
Object[] array2 = (Object[]) array;
System.out.println(array2.length);

or

Object array = new char[n];
char[] array2 = (char[]) array;
System.out.println(array2.length);

However if you have no idea what type of array it is you can use Array.getLength(Object);

System.out.println(Array.getLength(new boolean[4]);
System.out.println(Array.getLength(new int[5]);
System.out.println(Array.getLength(new String[6]);

Comments

1

Yes, it should be a field. And I think this value is assigned when you create your array (you have to choose the length of array while creating, for example: int[] a = new int[5];).

Comments

0

I believe its just a property as you access it as a property.

String[] s = new String[]{"abc","def","ghi"}
System.out.println(s.length)

returns 3

if it was a method then you would call s.length() right?

Comments

0

From the JLS:

The array's length is available as a final instance variable length

And:

Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.

And arrays are implemented in the JVM. You may want to look at the VM Spec for more info.

Comments

0

It is a public final field for the array type. You can refer to the document below:

http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7

Comments

0

Every array in java is considered as an object. The public final length is the data member which contains the number of components of the array (length may be positive or zero)

Comments

0

Java arrays, like C++ arrays, have the fixed length that after initializing it, you cannot change it. But, like class template vector - vector <T> - in C++ you can use Java class ArrayList that has many more utilities than Java arrays have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.