• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

length field on array ...

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
embeded (java definition, jvm implementation)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually length is not a field, but a literal.
 
Joe Ess
Bartender
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic