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

array length

saran sankar
Greenhorn

Joined: Jan 16, 2002
Posts: 7
Hi all,
When you want to know the array length, we are using array.length.But where is this length public variable is declared?
This variable is not available in the object class also?

Saran
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
It is an implicit property of the Java array primative. It is available to anybody with access to the array itself.
http://java.sun.com/docs/books/tutorial/java/data/arrays.html
http://java.sun.com/docs/books/tutorial/java/data/arraybasics.html#size
saran sankar
Greenhorn

Joined: Jan 16, 2002
Posts: 7
Hi steve,
Thanks for your reply.
But my concern is where the length is coming from.
In java arrays are objects.So where the length property is coming from?Where is the root?
Regards
Saran
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
An array is a primitive, not an object. The length of the array is an implicit property supported by Java.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Steve Deadsea:
An array is a primitive, not an object.

Actually, an array is an object in Java. The length of the array is an instance variable of the Array class. When constructing an array, like any other object in Java, you must use the keyword "new."
Corey


SCJP Tipline, etc.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
An array is an object in Java, but it is a special type of object which does not have an explicit class in the API, not to be confused with the class Array, which is more of a wrapper class to provide additional methods (but it has no fields of it's own).
An array is composed of the members that it inherits from Object and the final variable "length". This field is unique because it is not part of the "type" of the array, as most fields are (to prevent problems with putting arrays of differing sizes in the same reference variable). However, as programmers this is invisible, and you can get at the length just as though it were a regular field in the object.

From the JVM Spec

2.15.2 Array Variables
A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array.
Because an array's length is not part of its type, a single variable of array type may contain references to arrays of different lengths. Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.


"JavaRanch, where the deer and the Certified play" - David O'Meara
saran sankar
Greenhorn

Joined: Jan 16, 2002
Posts: 7
Hi Cindy Glass,
Thanks for your valuable information.
Regards
Saran
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array length