• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many Objects are created?

 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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



 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree. Try again.
Correct the compile-time error, then iterate through your outermost array and see what its elements are.
 
Sam Samson
Ranch Hand
Posts: 63
IntelliJ IDE Java jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is 9 correct?



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

And there should be one [][][]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think nine is incorrect.
[edit]Strikeout this post[/edit]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
IntelliJ IDE Java jQuery
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9497
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you think that by creating an array of primitives you also create Objects?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic