Stack memory
The stack memory stores a variable of the base type (int a = 3;) and a variable of the reference type (car car). When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable. The memory space can be used for other purposes immediately.
Heap Memory
Heap memory holds objects and arrays created by new, such as new auto;new int []; A reference variable is a normal variable that is defined when memory is allocated in the stack, and the reference variable is released in the program run to the extraterritorial scope. The array and object itself is allocated in the heap, and even if the program runs beyond the block of code that uses new to produce arrays and objects, the heap memory used by the array and the object itself will not be freed, and arrays and objects will become garbage when no reference variables point to them, but they also occupy memory. They are released at an indeterminate time by the garbage collector.
In addition to variables of 8 basic types (byte1 bytes int2 bytes short4 bytes Long8 bytes float4 bytes Double8 bytes char2 bytes boolean), other data types are referred to as reference data types, which is OBJECT.O The bject operation is all referenced by the object. Where references are a bit like pointers
Java heap memory, stack memory