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

Java Array

 
Sac Anand
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

In java, everything is in class, even main method can not be declared outside class like in C/C++.

Then where does length property (to find length of an array) comes from?

Also, when we declare a primitive array (or for object array), an object is created in heap memory, now every object is an instance of some class. Which class' object is created for arrays.

Regards,
Sachin Anand
 
Andrea Z�rcher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sachin

Yes, in Java arrays are always objects, regardless of what the array holds. I would say, an array is a complex data type.

Andrea
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:


You can possibly use Reflection on the class obtained by getClass() but that's far beyond the scope of the SCJP scope.
[ October 08, 2006: Message edited by: Barry Gaunt ]
 
Sac Anand
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Barry

I am preparing for SCJP and may be this is out of scope for the exam but i would really appreciate if somebody can throw some light on this. Actually, this question came across while my preparation for SCJP.

Below line of code gives "java.lang.Integer"

System.out.println(new Integer[0].getClass().getName());

But nowhere in Integer class there is length property.

Regards,
Sachin Anand
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not print "java.lang.Integer" but "[Ljava.lang.Integer;". That is the internal name of the Integer[] class. As I wrote previously you will have to learn to use the Reflection API to investigate further, or you will have to look at the Java source code.
 
Sac Anand
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright Barry, I'll do further investigation.

Thanks for your help.

Regards,
Sachin Anand
 
Praveen Babu
Ranch Hand
Posts: 138
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic