File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java Array" Watch "Java Array" New topic
Author

Java Array

Sac Anand
Greenhorn

Joined: Apr 21, 2006
Posts: 19
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

Joined: Oct 06, 2006
Posts: 10
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


SCJP 5.0
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sac Anand
Greenhorn

Joined: Apr 21, 2006
Posts: 19
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Apr 21, 2006
Posts: 19
Alright Barry, I'll do further investigation.

Thanks for your help.

Regards,
Sachin Anand
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
See this post,

http://www.coderanch.com/t/258624/java-programmer-SCJP/certification/length-property
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Array
 
Similar Threads
problem with collections
array length
synchronized block in static method
Array of zero length
Assigning a array of array to an Object reference