File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How does .length actually work? How would I go about finding out? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does .length actually work? How would I go about finding out?" Watch "How does .length actually work? How would I go about finding out?" New topic
Author

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

Jonathan Wallace
Greenhorn

Joined: Aug 18, 2010
Posts: 18
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

Joined: Oct 25, 2008
Posts: 2700

Length is a instance variable of a. Remember that arrays are objects.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jonathan Wallace
Greenhorn

Joined: Aug 18, 2010
Posts: 18
Hmm, I'm still confused as to how I should have known this, but thanks for clearing this up.

Thanks!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3495
    
  13
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.


Joanne
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
Don't get me started about those stupid light bulbs.
 
subject: How does .length actually work? How would I go about finding out?