• 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 attribute of array

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends

we know that if we want to get the no of elements in an array then we use the length attribute of the array.

So where is this attribute defined , I mean in which class ?

Can anyone help ?

Regards
Rohit
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An array is not strictly an object (though each does have an instance of java.reflect.Array backing it).
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Length is an attribute provided by JAVA Platform to all the Arrays.
 
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
The "length" attribute isn't a normal member field like you'd find in any other class. It's a feature of the Java language syntax. Code that
"reads" this field is compiled into bytecode that invokes a specific operation which fetches the length of an array.

Because it's not a real "member", it's not anywhere in the API Javadocs. Instead, you can read about it in any introductory Java book, or in the Java Language Specification here.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it make sense then to say that an array is an object but not an instance of any (known) class?
 
Ernest Friedman-Hill
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
Arrays really are objects, and each array is an instance of a specific class. If you call getClass() on an array, you'll get a Class object which represents that array's type; indeed, you can write

int[].class

to get the Class object that represents an array of ints.

Now, there are some funky parts. These classes don't have valid Java class names; they're similar to anonymous classes in this respect. They also don't have normal constructors, and they aren't created using the "new" opcode; you can't use Class.newInstance() or Constructor.newInstance() to create an array object.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic