• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many Objects are created?

 
Ranch Hand
Posts: 63
IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...



 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is 9 correct?



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

And there should be one [][][]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I think nine is incorrect.
[edit]Strikeout this post[/edit]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you think that by creating an array of primitives you also create Objects?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
reply
    Bookmark Topic Watch Topic
  • New Topic