• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many objects created for array init

 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes there will be single object on heap for each of the Array initializations under consideration.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic