*
The moose likes Beginning Java and the fly likes length field on array ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "length field on array ..." Watch "length field on array ..." New topic
Author

length field on array ...

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Hi,

We all know that we use arrayName.length to find out the length of the array. But from where this "length" field comes? We also know that arrays are similar to objects, to the extent we can invoke some methods also. But all such methods, fields are declared where?

I also searched the Object class, I found no "length" field specified...

Thanks for your answers,
Guru
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
embeded (java definition, jvm implementation)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Actually length is not a field, but a literal.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8867
    
    8

Originally posted by Ilja Preuss:
Actually length is not a field, but a literal.





The members of an array type are all of the following:

* The public final field length, which contains the number of components of the array (length may be positive or zero)
. . .

Java Language Specification Second Edition: Arrays 10.7

If length were a literal, it would be hardcoded in the JVM source and that would make creating arrays of various lengths difficult!


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Joe Ess:

If length were a literal, it would be hardcoded in the JVM source and that would make creating arrays of various lengths difficult!


Actually, it would be resolved by the compiler...

I guess I was confusing it with the class literal. Sorry for the confusion... :roll:
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Boy, this is a FAQ if there ever was one. I don't know how many times I'm heard this question. It's unfortunate, because for someone just starting out, "length" is very likely to be the first member-variable-looking thing they encounter, and it's incredibly frustrating that they can't find it in the API documentation.


[Jess in Action][AskingGoodQuestions]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: length field on array ...