Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does .length actually work? How would I go about finding out?

 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a.length returns the length of the array a, but how does it actually work and more importantly, how would I go about finding out? Normally this usage of the . operator would mean that length would be a method inside of an a object, right? Is that the case here? If so, where does the method come from? The api docs on array gives me a getLength method, but that seems like something else.

Can someone please explain this to me? :-) I understand how to use .length, but I find it frustrating not understanding what actually happens.

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Length is a instance variable of a. Remember that arrays are objects.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More accurately, length acts as if it's a final instance variable of the class of a. I say "acts as if" because in fact that's not what it is; the Java compiler turns a.length into an invocation of a specific JVM bytecode that fetches the length of an array. The implementation of that bytecode would be, of course, implementation-dependent, but in general the JVM will just store the length of each array object somewhere.

Note that in your example, your array is an object whose type is int[]. You can't find length in the Javadocs anywhere because this class int[] has no Javadoc page (nor does any other array class.) The documentation for length is therefore just in the Java Language Specification.
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, I'm still confused as to how I should have known this, but thanks for clearing this up.

Thanks!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Wallace wrote:Hmm, I'm still confused as to how I should have known this

By reading the Java Language Specification as Ernest suggested. Specifically Section 10.7.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Wallace wrote:Hmm, I'm still confused as to how I should have known this...


That's only a problem because you insisted on knowing it. For practical programming, all you have to know is what Wouter said. Knowing how things work may be interesting and is occasionally useful, but you can't expect standard training to go into diversions like that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic