One: Concept
The Java programming language divides memory into two types, called stack memory, which is called heap memory.
The Java heap is a run-time data area in which the objects of the class allocate space. These objects are created with the new directive, and they do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time.
The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible.
Two: examples
Operation Result:
Java Fundamentals: Stack memory and heap memory