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

background on array length

Daniel Dillon
Greenhorn

Joined: Aug 21, 2004
Posts: 1
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);
}
}
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
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...
 
 
subject: background on array length