Tuesday, 19 August 2014

How Objects are stored in heap and Stack in java


1. Stack is memory in computer which is used to store temporary information. For eg. variables are stored on stack.
2.Information after the method is done with its execution.
3. Stack is non- shared memory which means that data of a particular thread stored on stack can be seen only by the owner of data.

Overriding/method Overriding In java

Which variables are stored where?

Life Cycle of a thread 

There are mainly three types of variables:

1.Instance variables.
2. Static variables.
3. Primitive or local variables.


Instance variables : Objects and its instance variable are stored in heap (young generation area).

Primitive or local variables : Stack

Methods :Stack 

Reference of objects  : on stack

Static variable and static methods: on Permanent Generation area of heap

 1. Heap is mainly used to store objects.
2. Its a shared memory.
3. Both class instances and arrays are stored on heap.
4. This area of memory is shared by all threads.

You might interested in :

Android Hello World Program
Life Cycle of a thread 
Custom Exception in Java -Tutorial
Difference between equals() and = =