int x;
int y=10;
What type of memory is allocated in Java? I heard that everything in Java is allocated dynamic memory. That is true for objects, but does the same rule follow for primitive data types also (like int, float, etc.)?
In one line it depends on where the variable is declared.
Local variables (variables declared in method) are stored on the stack while instance and static variables are stored on the heap.*
NOTE: Type of the variable does not matter.
class A{
private int a = 10; ---> Will be allocated in heap
public void method(){
int b = 4; ----> Will be allocated in stack
}
}
primitive variables and function calls are stored in the stack. objects are stored in the heap.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With