aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stack n Heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stack n Heap" Watch "Stack n Heap" New topic
Author

Stack n Heap

usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
Hi,

can someone explain difference between stack n heap with examples? and when does stack n heap get used internally?

Thankz
newgen pras
Ranch Hand

Joined: Jun 01, 2005
Posts: 34
Hi ...
Stack is the place where ur primitive local varibles and object references get stored...

Heap is the place where ur object get stored ...

for example
Object ob = new Object();
Object ob1 = ob;

here ob, ob1 will get stored in stack..... the object created by new operator will stay in heap....

Pls correct me if i am wrong any way
Pramod Chris
Greenhorn

Joined: Oct 15, 2004
Posts: 21
Just to add, object references are stored in the stack if created within a method, else it is stored in the heap. Objects created are stored on the heap. Stack memory is the programs memory and Heap memory is outside the program.
newgen pras
Ranch Hand

Joined: Jun 01, 2005
Posts: 34
I don't think object reference will get stored in Heap... If that is going to be stored in Heap ..how it will get garbage collected ..pls explain me ...
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

I don't think object reference will get stored in Heap...
The Object reference 'o' is stored on the heap, as a member of the Bar instance.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi newgen

about your comment - "how it will get garbage collected",
I don't think GC would work that way. In fact it does run on "Heap" by starting from top level objects and then following object graphs to see which objects can be GCed.

Regards
Maulin
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11407
    
  16

The object REFERENCE is stored on the stack, but the object itself is in the heap ( think).


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

The object REFERENCE is stored on the stack, but the object itself is in the heap ( think).
See my previous reply. Local references are stored on the stack. Instance fields (references, primitives, etc.) are on the heap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack n Heap