Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

length variable in arrays....

 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class testArrayLength
{
private static int [] arr = new int[10];
public static void main(String ar[])
{
System.out.println(arr.length);
}
}

the output is 10...

please someone help me from where this variable length came from the API... how is the arr.length works..??? and that too it is length and not LENGTH... all are of lower case.. please help.. thanks in advance...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
length is a final field associated with arrays.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when arr.length works it means that length is associated with arrays.. but how and where do the length variable come from??? pls help me...
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are objects, but there is no class definition associated with it. They inherit some methods from Object and when created the length field is initialized.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array's are not in the API, but are described in the Java Language Specification.

"The array's length is available as a final instance variable length." (Ref: Section 10.3 - Array Creation).

Also see Section 10.7 - Array Members.
[ April 05, 2006: Message edited by: marc weber ]
 
kalps ganvir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
length is benificial var that will return u the length of array .getting !!
so explore ur skills of using it. instead of unnessesarily findng its history.




'MAN IS CREATURE WHO TAKES MORE INTREST IN TEACHING INSTEAD OF LEARNING NEW THINGS'

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic