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 ?
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.
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.
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.