Are you guys sure? I would imagine that when you do: int x = new int; 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.
Joined: Oct 17, 2006
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 ]
Joined: Dec 16, 2008
Yes, but isn't int x = new int; different from int x = new int; ?
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