(a), stack, heap
1. Register: The fastest storage area, which is allocated by the compiler according to the requirements, we can not control in the program.
2. Stack: A reference to a basic type of variable data and an object, but the object itself is not stored in the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool.
3. Heap: Stores all new objects.
4. Static domain: holds static member (static defined)
5. Constant pool: Holds string constants and basic type constants (public Static final).
6. Non-RAM storage: Persistent storage space such as hard disks
Here we mainly care about stacks, heaps and constant pools, for stacks and constants in pools where objects can be shared, and objects in the heap cannot be shared. The data size and life cycle in the stack can be determined, and the data disappears when no references point to the data. The garbage collector is responsible for the collection of objects in the heap, so the size and life cycle do not need to be determined and have great flexibility.
for strings: References to objects are stored in the stack, and if the compilation period has been created (defined directly in double quotes), it is stored in a constant pool, which is stored in the heap if the run-time (new) is determined. For a string equal to equals, there is always only one copy in the constant pool, with multiple copies in the heap.
(ii) Stacks in Java memory allocation
Some basic types of variable data and object reference variables that are defined in the function are allocated in the function's stack memory. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits its scope, Java automatically frees the memory space allocated for that variable, and the memory space can be used immediately by another.
Stacks and memory allocations in Java