• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array length - JLS  RSS feed

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS


An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. The array's length is available as a final instance variable length.


In general I understand that arrayname.length will give me the length of the array.

But I am a bit confused with the term "final instance variable length".
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"length" is constant; it appears as if it were declared as a final member variable in the array "class":



An "instance variable" is a member variable that's not a class (static) variable.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

Thanks for the explanation. Is that how it is internally represented ? You did mention that it is not a valid Java code, but is it true that internal implementation of "length" is something similar.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EFH]: "length" is constant

It's constant in the sense that, for a given array instance, it does not change. However I'd note that it is not a compile-time constant. Often people say "constant" as shorthand for "compile-time constant" - just wanted to note that such usage would not be applicable here.

[arnb]: is it true that internal implementation of "length" is something similar.

The internal representation is something very similar. Each array instance probably has about 4 bytes of space set aside for the length, which is probably just like a final int instance variable except that it probably wasn't actually declared in Java originally; more likely the implementation of this is written in C. At least that's the case for Sun's JVMs. One difference between length and a "real" instance variable is that length does not appear as a field if you use reflection to find the fields of an array object. Other than that, I think you can just pretend that yes, the length is defined as a final int nonstatic field in the array class, and that is close enough to true that any other differences are unimportant.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim

Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!