Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array length

 
saran sankar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array is a primitive, not an object. The length of the array is an implicit property supported by Java.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saran sankar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cindy Glass,
Thanks for your valuable information.
Regards
Saran
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic