There are 2 types of variables in Java, namely primitive & reference.
Primitive Type
Primitive-type variables are always stored inside the stack memory. Stack is a memory space that holds method-level variables that are short-lived in addition to references to other objects in the heap.
Example of primitive type variables:
Data Type | Default Value (for fields) |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | ‘\u0000’ |
String (or any object) | null |
boolean | false |
Reference Type
Reference type variables aka objects or pointers are stored in two locations. The actual object data is stored inside the heap memory (the memory space that holds objects and JRE classes), and a reference aka pointer for the object is kept inside stack memory, which just points to the actual object.