Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Array length ..

 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array is defined as
int[ ] x = new int[5];
then x.length will give value 5,
what is length? method / data member and if so then of which class (surely it will not method)
regards,
sujit
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For Arrays - "length" is data members which will specify the number of elements in the Array.

"length" can not be a method as it does not use () while being called.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All arrays are an object of the Array class. length is a data member of that class. It can be a data mamber because this information is set. Try this

Hope This Helps
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int [] x = new int[5];
x.getClass().getName() gives [I
if(x instanceof Array) --> this gives error Class Array Not Found
I have checked, class Array is in java.lang.reflect package
what is class for array object and what is length ?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eventhough arrays are objects, they are instantiated without the class name, which means they don't belong to any class as such. Since arrays are objects, x instanceof Object returns true.
The java.lang.reflect.Array class provides static methods to dynamically create and access Java arrays. It is for a different purpose.
Ajith
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ajith,
if x is array, then (x instanceof Object) gives result true.
Now it is clear to me.
Still I have doubt about .length ?
length is member of which class ? It has to be memeber of some class.
x.getClass().getName() gives result [I , what is meaning of [I
sujit
[This message has been edited by Sujit Kurtadikar (edited January 17, 2001).]
[This message has been edited by Sujit Kurtadikar (edited January 17, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic