#26 Stack And Heap in Java
Telusko
JVM Memory Structure
Let's take a look at the memory structure of the JVM.
Firstly, we have the stack memory, which stores data in
a last-in-first-out sequence. Secondly, we have the
heap memory, which has an open space to store data
in.
Local vs Instance Variables
There are two types of variables: local and instance.
Local variables are declared within a method, whereas
instance variables are declared within a class. In the
example of a calculator, the variables n1 and n2 are
local, and num is an instance variable.
Function Calling and Data Areas
When a function is called, it creates a new stack in
memory. The add method has its own stack with n1 and
n2 variables, and num is not a part of either the add or
main stack. The heap memory is used to store the
reference variable obj.
Telusko
JVM Memory Structure
Let's take a look at the memory structure of the JVM.
Firstly, we have the stack memory, which stores data in
a last-in-first-out sequence. Secondly, we have the
heap memory, which has an open space to store data
in.
Local vs Instance Variables
There are two types of variables: local and instance.
Local variables are declared within a method, whereas
instance variables are declared within a class. In the
example of a calculator, the variables n1 and n2 are
local, and num is an instance variable.
Function Calling and Data Areas
When a function is called, it creates a new stack in
memory. The add method has its own stack with n1 and
n2 variables, and num is not a part of either the add or
main stack. The heap memory is used to store the
reference variable obj.