Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# How many objects?

Jeff Schuler
Greenhorn
Posts: 23
How many objects are on the heap after this line executes?

---------
int[][] dots3 = new int[3][];
---------

How many objects are on the heap after this line executes (assume different program)?

---------
int[][] dots3 = new int[3][4];
---------

Are the answers 1 and 5 respectively?
[ June 20, 2007: Message edited by: Jeff Schuler ]

Manfred Klug
Ranch Hand
Posts: 377
Originally posted by Jeff Schuler:

Yes
and 5 respectively?

No

Raghavan Muthu
Ranch Hand
Posts: 3381
No,in both the cases there is only one object created.

You may get confused by just seeing the dimensions get varying, but infact they are just the placeholders for the size (room allocation).

Until and unless you manually create an object and assign to the index of the array, there are NO extra objects created.

Say, for example

Now, there are 3 objects

• one object represented by the 2d array
• two objects represented each by the 1d arrays

• HtH.

Manfred Klug
Ranch Hand
Posts: 377
Originally posted by Raghavan Muthu:
int[][] int2DArray = new int[2][2]; //here only one object!

Not correct. Remember, on the heap are all arrays one dimensional.
Originally posted by Raghavan Muthu:

/* Assigning the arrays to the 2D array object */
int2DArray[0] = int1DArray1;
int2DArray[1] = int1DArray2;
[/CODE]

And those two statements give you the hint how multidimensional arrays are organized.

If you have doubts, have a look at 'Creating, Initializing, and Accessing an Array' in the Java tutorial.

raj malhotra
Ranch Hand
Posts: 288
I think only one object will be created only the dots3 reference variable will be provided enough space to hold reference of the 3 individual arrays of size 4 which are not created yet.
[ June 21, 2007: Message edited by: raj malhotra ]

Manfred Klug
Ranch Hand
Posts: 377
Hi raj,

which of the two definitions do you refer to?

or

[ June 21, 2007: Message edited by: Manfred Klug ]

Raghavan Muthu
Ranch Hand
Posts: 3381
Hello Manfred,

Originally posted by Manfred Klug:
Not correct. Remember, on the heap are all arrays one dimensional.

I haven't mentioned anything about the dimensions while saying about the object counts. I just said that, "there is only one object created"!..

I do agree with you that the array itself is just an object and nowhere it holds the dimensions for object!! But i have not mentioned that a 2D object is created.

Originally posted by Manfred Klug:
And those two statements give you the hint how multidimensional arrays are organized.

What exactly you are trying to say? Can you please elaborate a bit more on this?

Raghavan Muthu
Ranch Hand
Posts: 3381
Hi Raj,

Originally posted by Raj malhotra:
I think only one object will be created only the dots3 reference variable will be provided enough space to hold reference of the 3 individual arrays of size 4 which are not created yet.

Yes, thats perfectly correct.

Originally posted by Manfred Klug:
which of the two definitions do you refer to?

Manfred, Raj's statements seems to be referring to the second one which holds 3*4 dimension.

Manfred Klug
Ranch Hand
Posts: 377
Originally posted by Raghavan Muthu:
What exactly you are trying to say? Can you please elaborate a bit more on this?

Quote from 'Creating, Initializing, and Accessing an Array' in the Java tutorial
In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran.

As consequence, the statement creates four objects.

One array of array and three arrays of int.

Ranch Hand
Posts: 2874
And Manfred is right. Cheers.

Riya Pant
Greenhorn
Posts: 28
Hi,
I am still not very clear.Let me put it again what I understand:
If we write
int[][] array = new int[3][]; Then 1 object is created.
And if I write
int[][] array = new int[3][4]; Then 4 objects are created.
Am I right?

Manfred Klug
Ranch Hand
Posts: 377
Originally posted by Riya Pant:
Am I right?

Yes.

Raghavan Muthu
Ranch Hand
Posts: 3381
That's fine Manfred. Thanks.

int[][] int2DArray = new int[2][2]; //here only one object!

But i was intended to say only one object for the 2D array object "int2DArray". I should have made a note in the comment!

During the second statement of OP, it creates 4 objects. You are right. This holds good when you fill both the dimensions for the 2D Array (or all n dimensions for a nD Array).

Sergio Tridente
Ranch Hand
Posts: 329
Hi Raghavan,

Take a look at this modified version of your previous example:

This runs fine too, which confirms what Manfred said before.

[ June 21, 2007: Message edited by: Sergio Tridente ]

Manfred Klug
Ranch Hand
Posts: 377
Hi Raghavan,

the problem was the following statement:
No,in both the cases there is only one object created.

Riya Pant
Greenhorn
Posts: 28
Thanks everyone for valuable inputs

Raghavan Muthu
Ranch Hand
Posts: 3381
Hi Sergio,

Thats fine. I had a small confusion and that was a mistake. I had told in one of the previous replies.

During the second statement of OP, it creates 4 objects. You are right. This holds good when you fill both the dimensions for the 2D Array (or all n dimensions for a nD Array).

I dont get your statement below..

Originally posted by Sergio:

Can you clarify?

Raghavan Muthu
Ranch Hand
Posts: 3381
Hi Manfred,

Hi Raghavan,

the problem was the following statement:

quote:No,in both the cases there is only one object created.

Yes, that was a mistake I had corrected the same and mentioned in my previous reply.

Thanks again for pointing out!

Sergio Tridente
Ranch Hand
Posts: 329
Originally posted by Raghavan Muthu:
Can you clarify?

Don't worry. I was just refering to your post just above mine. (When I began to answer it wasn't there, so I apologized for repeating what you had just said.)
[ June 21, 2007: Message edited by: Sergio Tridente ]

Raghavan Muthu
Ranch Hand
Posts: 3381
Thats fine Sergio. It happens sometimes