Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Heap o' objects

 
Greg Neef
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take the following:
int [][] myArray = new int [3][] ;
myArray[0] = new int[2] ;
myArray[0][0] = 6 ;
myArray[0][1] = 7 ;
Do I understand correctly that there are two objects on the heap, those being the two arrays declared with the new? The myArray reference is on the stack then and the ints in myArray[][] are also on on the heap but contained within the already allocated array object?
Would the # of stack vs.heap objects differ if I was not using primatives? For example,
Thing [][] myThingArray = new Thing [3][] ;
myThingArray[0] = new Thing[2] ;
myThingArray[0][0] = new Thing( 6 ) ;
myThingArray[0][1] = new Thing( 7 ) ;
I assume that now I have two additional heap objects associated with the new Thing(s) referred to by myThingArray[0][0] and myThingArray[0][1] but still only one stack object.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,
Welcome to JavaRanch. Assuming that myArray and myThingArray are method locals and not instance variables, then I'd say you have a pretty good handle on what's going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic