This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
More accurately, lengthacts 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 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.