• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

length field on array ...

 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
embeded (java definition, jvm implementation)
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually length is not a field, but a literal.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic