Heap Area :
- in java ,
Object Present in Heap Area.
- Object having 2 things that is
Non - static field(s) and Non - static method(s).
- So, Non - static field(s) and non - static method(s) also present in heap area.
Stack Area :
- Stack Area follows
First In Last Out (FILO) or Last In First Out(LIFO) technique .
- In java ,
local variable(s) & parameter variable(s) present in stack area .
- Each method having individual stack memory in Stack Area and it follow LIFO technique .
- Means If method execution completed then delete from the Stack Area.
Note :
- Primitive Field(s) / Primitive variable(s) holds direct value .
- Reference Field(s) / Reference variable(s) holds reference of Object instead of direct value .
public class Test
{
Test t;
int val;
public Test(int val)
{
this.val = val;
}
public Test(int val, Test t)
{
this.val = val;
this.t = t;
}
public static void main(String[] args)
{
Test t1 = new Test(100);
Test t2 = new Test(200, t1);
Test t3 = new Test(300, t1);
Test t4 = new Test(400, t2);
t2.t = t3;
t3.t = t4;
t1.t = t2;
t2.t = t4.t;
System.out.println(t1.t.val);
System.out.println(t2.t.val);
System.out.println(t3.t.val);
System.out.println(t4.t.val);
}
}
Output :
200
200
400
200

Top comments (0)