File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes doubts on constructor, length wrt arrays in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "doubts on constructor, length wrt arrays in Java" Watch "doubts on constructor, length wrt arrays in Java" New topic

doubts on constructor, length wrt arrays in Java

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


I have some doubts with respect to arrays in Java. (not the java.util.Arrays class).

(i) Whenever we construct an array using new operator, there has to be a constructor invoked at one point of time. But the constructor of the class (which is instantiated for the array of objects) is not called, eventhough its present.

Since arrays in Java are direct descendants of java.lang.Object class (JSL confirms that), it should atleast definitely invoke the constructor of java.lang.Object class right? But how come we get to know that? There is no Constructor in Object class as well.

If at all, there is a constructor present in Object class, that should be invoked during array construction. Is that right?

For a reference, you may please have a look at this thread.

(ii) From where the length variable is being provided for the arrays ? As it is not present in java.lang.Object class.

[ July 09, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Beginner question, it seems to me. Anyway...

Java does not have arrays of objects, only arrays of object references or primitives. When an array of object references is constructed, it is initialised so that all the references are null. So, apart from the array itself, there are no objects to construct.

The java.lang.Object class does have a constructor. There is just one constructor for that class, taking no arguments. You can't see the code for it, because it's native (obviously?).

Array classes exist implicitly for every class of object. However, you will not see a class file or documentation for any array class. Conceptually, the constructor of java.lang.Object is called when an array is constructed; whether this is really true is probably JVM implementation dependent.

Array classes have a "length" field, basically because they are specified to have one. Since array classes exist implicitly and are coded in native code, you won't find any Java implementation of it.

Basically, all this stuff is just the low-level get-you-started stuff that Java provides and on which the rest of Java is built. It's how it is because the Java Language Spec says so, and that's that!
[ July 09, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thank you Peter for confirming my assumptions.

Sorry, i was intended to just say the class whose object references are being stored in array but i missed to mention the important term "references"

So, we need to believe or assume that these things happen with no proof! Is it?

Thanks again.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
What do you mean by proof?

The Java Language Specification tells you what behaviours Object and arrays will provide. You have to trust that your JVM will behave as specified; if it's Sun's or another popular vendor's JVM then, apart from a very few bugs, it will do so.

As Java is now becoming open-source, I suppose it is theoretically possible to go find out how these features are implemented. I wouldn't bother, personally!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There seem to be a couple questions going on here, and I'm not real sure which is the real issue.

One (maybe) - When you make a new array the JVM does create an instance of some class, but it's a special class known to the compiler and the native runtime, not one we usually see or touch. Try this to see the special name it gets:

There may well be something constructor-like in that special class, but it's in a don't know and don't care category for me.

Two (or not) - Are you asking why we see no MyClass constructors when we create a new MyClass[5] array? No instances of MyClass have been created yet, only references that are so far null, pointing to nothing. We might follow that up with a loop to create 5 new instances, and then you'd see the constructors run.

Did either of those match your concerns?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thank you Peter and Stan James for the explanations.

Yes, i do agree with you. We need to go with the belief.

Peter, by the term "proof", i meant to say the confirmed outcome which we are able to see or touch. As you said, as its with native we can neither see nor touch that code.

Thats the way, we need to go ahead with the belief on the JVM's behaviour.

Thanks again.
I agree. Here's the link:
subject: doubts on constructor, length wrt arrays in Java
Similar Threads
Sun Cirtification
Practice Question
String.length() vs. Arrays.length
Is every array an Object?