File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Heap o' objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Heap o Watch "Heap o New topic

Heap o' objects

Greg Neef
Ranch Hand

Joined: Jun 16, 2003
Posts: 82
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.

SCJP 1.4
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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.

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link:
subject: Heap o' objects
It's not a secret anymore!