Memory mechanism:
The memory mechanism of Java is divided into heap memory, stack memory, method area and so on.
The difference between the two:
Heap memory primarily holds entity objects, which are used to hold new objects and arrays of entity objects that reference variables. Managed by the virtual machine automatic garbage collector.
Stack memory primarily stores variables of basic basic types.
The method area mainly stores a class template and a static method.
Different location of the declaration determines where to store
Declaring in a method (local variable): Allocating space to a variable in the stack when called, releasing the stack at the end, and embodying the limitations of the variable.
Declare in a class (member variable): The variable is stored in the heap due to the need for the cycle of the action. is not invalidated by method destruction, similar to global variables in C.
For reference variables, the value stored in the corresponding memory is a reference, which is the storage address of the object and is stored in the stack.
Java basic data Type storage location