aspose file tools*
The moose likes Beginning Java and the fly likes Reference Type Stored On Heap Or Stack? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reference Type Stored On Heap Or Stack?" Watch "Reference Type Stored On Heap Or Stack?" New topic
Author

Reference Type Stored On Heap Or Stack?

nikunj jhala
Greenhorn

Joined: Nov 18, 2006
Posts: 9
Hi,

i know that reference type stored on heap becz compiler will take time to create memory for object first time, but after that because Heap provide random access for that object stored on heap. value means int,float etc stored on Stack but why Reference type not stored on Stack ? (according to me, becz Stack follow LIFO algo..).
please clear my concept if possible give me correct answer ?

thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just to clarify terminology, we'd usually call what you're asking about object instances or just objects. Object instances live on the heap so they can exist beyond the life span or scope of the current method or code block. Anything that's put on the stack in a code block is lost when the block exits. An object on the heap lives as long as there is any reference to it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
nikunj jhala
Greenhorn

Joined: Nov 18, 2006
Posts: 9
Originally posted by Stan James:
Just to clarify terminology, we'd usually call what you're asking about object instances or just objects. Object instances live on the heap so they can exist beyond the life span or scope of the current method or code block. Anything that's put on the stack in a code block is lost when the block exits. An object on the heap lives as long as there is any reference to it.


thanks,

i don't know that you teach me that heap hold object for long time , i know the instance life time,
i Appreciate your knowledge.i will send you more this type of Q.
thanks,

Nikunj Jhala
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Stan James:
An object on the heap lives as long as there is any reference to it.


An object continues to live in the heap even if there are no references (live references) to it untill the GC collects it.


Rahul Bhattacharjee
LinkedIn - Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Technically correct, dependending on our definitions of "alive" I guess. Since I can't see GC sweep something away, I can generally act as though it's "dead" as soon as it's out of scope.

One of the inventors of Simula, predecessor to Smalltalk and all of OO, said all they did was move some data from the stack to the heap. They didn't think it was that big a deal. It's a cute line and I'm sure he said it with a wink and smile. Still it describes one critical aspect of object stuff pretty well.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

An object must be in the heap. On the other hand, an object reference can be either on the stack (as a local variable) or in the heap (as an instance variable of another object).

-- Kaydell
[ November 25, 2006: Message edited by: Kaydell Leavitt ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reference Type Stored On Heap Or Stack?