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

doubts on constructor, length wrt arrays in Java

 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

TIA.
[ July 09, 2007: Message edited by: Raghavan Muthu ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic