• 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

background on array length

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I am quite new to java. I have taken one class, and have done some reading, but am still putting things together.

Listed below is a small bit of code which creates an integer array, then finds and displays the length of the array. I am trying to find out more about the "intArray.length" construct. I understand that an array is an object. How do I look for the array object in the Java API? What I did find is an Array class in the java.lang.reflect package, which extends the Object class, if I am understanding it right. When I look for instance variables in the Array class, I do not find one. Under the methods, there was "getLength", but not length. Also, it was my general understanding that a method always ends with parenthesis.

public class testing2 {

public static void main (String[] args) {

int[] intArray = {1,2,3};
int intArrayLength = intArray.length;
System.out.println("length of intArray is " + intArrayLength);
}
}
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Daniel!

The J2SE API documentation doesn't cover arrays. There is no class definition for an array object.

length is a public final member of an array object, assigned a value when the array object is created.

Some other folks around these parts have discussed this topic a few times before. You might like to do a quick search on this forum for "array length". Note that the search page link is towards the top right of this page.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there.

Well you are right, the methods have a parenthesis at the end. But the lenght of an array is not a returned by a method. Well, if you observe closely in the api of Arrays, you'll find that lenght is not mentioned in the methods, but as a class variable. Well, now u know what a variable is. Here length is a variable of int type. A simple
int x = length;
will give u an error, but
int x = array.length;
will give u the length of that array. You'll come across class variables more often once you start programming applets. In applets, the colors are class variables and not methods.

Hang on with Java and u'll soon understand the jargon.

Happy Programming...
reply
    Bookmark Topic Watch Topic
  • New Topic