Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reference Type Stored On Heap Or Stack?

 
nikunj jhala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikunj jhala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic