No, I think nine is incorrect. [edit]Strikeout this post[/edit]
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
4
posted
0
In that case, I was mistaken and 9 does appear to be correct. I would have thought that outer array would be filled with nulls; I was obviously mistaken. Sorry.
Sam Samson wrote:Simple question I guess, but I don't get it. How many Objects are created? My tip is 11...
Seems you have your answer; and now you can forget it, because the chances are that you will never, ever have any use for it in your life as a Java programmer.
That's what profilers are for
Winston
Isn't it funny how there's always time and money enough to do it WRONG?
Campbell Ritchie wrote:In that case, I was mistaken and 9 does appear to be correct. I would have thought that outer array would be filled with nulls; I was obviously mistaken. Sorry.
It would be with the syntax int[][][] numbers = new int[2][][], but the given syntax allows you to fully populate multi-dimensional arrays*, so only the lowest level is filled with the default values (zero, in this case).
(* Yes, that was just to wind you up! To be fair, initialising them like this is the one time it almost makes sense to refer to them as multi-dimensional arrays rather than arrays-of-arrays-of-arrays-of...)
Sam Samson wrote:I know that multi-dimensional arrays are very rare, luckily...
I wasn't referring to the rareness of multi-dimensional arrays, but to the knowledge of how many objects are created. You usually only have to worry about that stuff when you get OutOfMemoryError's which, I'm happy to report, I've so far never encountered in 11 years of writing Java.
Winston
Jimmy Clark
Ranch Hand
Joined: Apr 16, 2008
Posts: 2187
posted
0
Do you think that by creating an array of primitives you also create Objects?
Sam Samson wrote:I know that multi-dimensional arrays are very rare, luckily...
I wasn't referring to the rareness of multi-dimensional arrays, but to the knowledge of how many objects are created. You usually only have to worry about that stuff when you get OutOfMemoryError's
And even then, we don't care about "How many objects does this line of code create?" Rather, we look for, "Where are there potentially a lot of objects that we're hanging onto that we don't need?"
I've encountered a handful of OOMEs in my years of Java programming, but I have never, ever--even when tracking down those OOMEs--been concerned with figuring out exactly how many objects a given line of code creates.