45

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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
source.rar
  • 8,002
  • 10
  • 50
  • 82

9 Answers9

44

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.

evlogii
  • 811
  • 1
  • 7
  • 17
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 4
    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'. – source.rar May 10 '11 at 14:19
  • 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? – Chao Aug 22 '16 at 13:31
  • Okay, it says in the design document but I check Java source code, I don't see the implementation? Why? – Weijing Jay Lin Mar 07 '21 at 20:29
  • @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? – Joachim Sauer Mar 07 '21 at 22:56
  • @JoachimSauer Okay, I guess it on Java Engine side, in c/c++? – Weijing Jay Lin Mar 08 '21 at 23:09
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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 8
    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 May 10 '11 at 12:52
  • 2
    @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. http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#64347 – Bill the Lizard May 10 '11 at 12:58
  • 1
    From the *language* perspective it is clearly a field: it behaves exactly the same as all other `public final` fields. – Joachim Sauer May 10 '11 at 13:00
  • 8
    @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 May 10 '11 at 13:08
  • 3
    @bestsss: you're **absolutely right**. But most people (especially beginners) don't *care* about anything below the language level ;-) – Joachim Sauer May 10 '11 at 13:13
  • @Joachim, my remark targeted Bill mostly, hopefully he is no a beginner... and ughh I live in a universe where the java recruits come with some strong assembler background. – bestsss May 10 '11 at 13:18
  • @bestsss: that sounds like an ... *interesting* universe. I can imagine that being either very, very cool or quite abysmal, depending on which way they choose to go with that knowledge. – Joachim Sauer May 10 '11 at 13:20
  • @bestsss: I'm no stranger to assembly, although I try not to get it in my Java. :) I have thought about writing a compiler for the JVM, but that's a project for when I have more free time (so maybe never, unfortunately). – Bill the Lizard May 10 '11 at 13:41
  • @Bill, I, myself, believe I am not good enough to write a full-feldged (and fast) JVM. Actually I was thinking it'd be sort of cool to write a JVM in java itself, that can output both native code and java. (opposed to C/C++ JVM). While it'd need a C/C++ to bootstrap the underlying JVM can be stupid enough. – bestsss May 10 '11 at 15:32
  • 1
    @bestsss: I was thinking of something a bit more modest, like a BF-to-JVM bytecode compiler, would be interesting. [BF](http://www.muppetlabs.com/~breadbox/bf/) itself has very few instructions, and it would be interesting to see how few JVM instructions would be needed to write a compiler for it. – Bill the Lizard May 10 '11 at 15:56
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]);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
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];).

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
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.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Infintyyy
  • 929
  • 1
  • 11
  • 23
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?

netbrain
  • 9,194
  • 6
  • 42
  • 68
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.

planetjones
  • 12,469
  • 5
  • 50
  • 51
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

James Jithin
  • 10,183
  • 5
  • 36
  • 51
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)

Manimaran Selvan
  • 2,166
  • 1
  • 14
  • 14