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.
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.
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?
Joined: Oct 30, 2001
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!
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 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