This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How many Objects are created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many Objects are created?" Watch "How many Objects are created?" New topic
Author

How many Objects are created?

Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

Hi

Simple question I guess, but I don't get it. How many Objects are created? My tip is 11...



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Disagree. Try again.
Correct the compile-time error, then iterate through your outermost array and see what its elements are.
Sam Samson
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

Is 9 correct?



Output:
[][]
[]
[]
[]
[][]
[]
[]
[]

And there should be one [][][]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
No, I think nine is incorrect.
[edit]Strikeout this post[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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
Ranch Hand

Joined: Oct 08, 2011
Posts: 61

So a rule could be:
1 [][][] + 2[][] + 2*3[] = 9

I know that multi-dimensional arrays are very rare, luckily, because it's just a brain **** (to me).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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
Do you think that by creating an array of primitives you also create Objects?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jimmy Clark wrote:Do you think that by creating an array of primitives you also create Objects?


I'm not sure who that's directed at, but I didn't see any evidence here that anybody thinks that. (Except of course for the array object itself.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How many Objects are created?
 
Similar Threads
Objects created on Heap
How many objects?
object creation in GC
How many references
how many objects will be created.