aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects created for array init Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many objects created for array init" Watch "How many objects created for array init" New topic
Author

How many objects created for array init

Erez Pitchon
Greenhorn

Joined: Dec 19, 2008
Posts: 13
Hi

From reviewing Siera Bates SCJP study guide, the following would create a single object on the heap:

int[] x = new int[5]

How many objects would each of the following create and what would be their composition?

1. int[][] x = new int[5][]

2. int[][] x = new int[5][3]

Would the same answer apply to object type arrays?

i.e. Foo[][] x = new Foo[5][3]

Thanks so much for your expertise.
geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
Here in all the cases quoted by you, there is only one object on the heap. Array objects are created when you initialize with new keyword

Thanks,
Geeta Vemula
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Yes there will be single object on heap for each of the Array initializations under consideration.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Are you guys sure? I would imagine that when you do:
int[][] x = new int[5][3];
There is actually 6 objects created, since the JVM not only needs to create the array capable of holding 5 int[] arrays, but then it has to create the 5 arrays capable of holding 3 ints each.

That is a shorthand to doing:


Does this sound right, or is there any flaw in my reasoning?

To answer the second part of the original question:

When reference arrays are created, each element is automatically initialized to null, so there is no object creation involved, and the answer would be the same no matter the type of element of the array (primitive or reference type.) The only exception to this case is when multidimensional arrays (arrays of arrays, an array being a particular form of a reference, since arrays are objects) are created, and you specify any dimension size beyond the first one (this is the case illustrated above.)
[ December 19, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
I am sure. Because with

JVM is instructed to create an array of 5 elements where each element can hold a new array. But JVM is not told to create the arrays for different elements. So, only one array will be created as well as one object.
When

are issued, JVM will create objects for new arrays.
[ December 19, 2008: Message edited by: Rajshekhar Paul ]
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yes, but isn't
int[][] x = new int[5][3];
different from
int[][] x = new int[5][]; ?

As a matter of fact, I think that any statement of the form:

X[][] x = new X[a1][a2]...[an]
will instantiate 1 + (a1) + (a1)*(a2) + (a1)*(a2)*...*(an-1) arrays

For this code:

I am getting this output:

[[I@1a46e30
[[I@3e25a5
[[LArrayCreation;@19821f
[LArrayCreation;@addbf1
[LArrayCreation;@42e816
[LArrayCreation;@9304b1
[LArrayCreation;@190d11
[LArrayCreation;@a90653

[ December 19, 2008: Message edited by: Ruben Soto ]
[ December 19, 2008: Message edited by: Ruben Soto ]
Erez Pitchon
Greenhorn

Joined: Dec 19, 2008
Posts: 13
I tend to think so as well. If not, what would the compiler do extra when the size of the second dimension is specified?
i.e.

What would the compiler do here with the "3"
int[][] x = new int[5][3];

...that it did not do here
int[][] x = new int[5][]; ?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Exactly.
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
It's correct that there will be more than one object when you specify the size of the element arrays because that time, JVM will create array objects for element arrays also.

In both the above cases, there will be 6 objects on the heap, all are array objects.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya Ruben is right here.

int[][] x = new int[5][3];


One array will created for holding address of 5 int[](int arrays);
And 5 different int[] array of size 3 will be created for holding actual elements.

like:

|0|1|2|3|4| one object.
|
|--->|0|1|2| such 5 objects.



so total 6 object will be created.

[ December 19, 2008: Message edited by: punit singh ]
[ December 19, 2008: Message edited by: punit singh ]

SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

How many objects would each of the following create and what would be their composition?

1. int[][] x = new int[5][]


One array of size 5, those elements are arrays of ints. Total is one object.

2. int[][] x = new int[5][3]


One array of size 5, whose elements are arrays of ints. Five arrays of size 3, whose elements are ints. Total is six objects.

Would the same answer apply to object type arrays?

i.e. Foo[][] x = new Foo[5][3]


One array of size 5, whose elements are arrays of Foo objects. Five arrays of size 3, whose elements are Foo objects. No Foo objects are instantiated. Total is six objects.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many objects created for array init