A detailed introduction to the heap, stack and constant pool in Java

The following mainly introduces the heap, stack and constant pool in JAVA:


1. Register The


fastest storage area is allocated by the compiler according to the needs, and we cannot control it in the program.


2. Stack


Stores basic types of variable data and object references, but the object itself is not stored in the stack, but in the heap (the new object) or the constant pool (string constant objects are stored in the constant pool.)


3. Heap


Stores all new objects.


4. Static domain to


store static members (static definition)


5. Constant pool to


store string constants and basic type constants (public static final).


6. Non-RAM storage


Hard disk and other permanent storage space


Here we mainly care about stack, heap and constant pool. Objects in stack and constant pool can be shared, but objects in heap cannot be shared. The size and life cycle of the data in the stack can be determined. When there is no reference to the data, the data will disappear. The objects in the heap are collected by the garbage collector, so the size and life cycle do not need to be determined, which has great flexibility.


For strings: the references to their objects are stored in the stack. If they are created at compile time (defined directly with double quotes), they are stored in the constant pool. If they are created at runtime (new), they can be determined. are stored in the heap. For strings whose equals are equal, there is always only one copy in the constant pool and multiple copies in the heap.


Such as the following code:
String s1 = "china"; String s2 = "china"; String s3 = "china"; String ss1 = new String("china"); String ss2 = new String("china"); String ss3 = new String(" china");


for variables and constants of primitive types: variables and references are stored on the stack, and constants are stored in the constant pool.


Such as the following code:
int i1 = 9; int i2 = 9; int i3 = 9; public static final int INT1 = 9; public static final int INT2 = 9; public static final int INT3 = 9;


for member variables and local variables: Member variables are the variables defined outside the method and inside the class; local variables are the variables defined inside the method or statement block. Local variables must be initialized. Formal parameters are local variables, and the data of local variables exist in stack memory. Local variables in stack memory disappear as the method disappears.


Member variables are stored in objects on the heap and are collected by the garbage collector.


Such as the following code:
class BirthDate { privateint day; privateint month; privateint year; public BirthDate(int d, int m, int y) { day = d; month = m; year = y; main(String args[]) { int date = 9; Test test = new Test(); test.change(date); BirthDate d1 = new BirthDate(7,7,1970); } publicvoid change1(int i){ i = 1234;


For the above code, date is a local variable, i, d, m, y are all local variables, and day, month, and year are member variables. Let's analyze the changes when the code is executed:


1. The main method starts to execute:
int date = 9;
date local variables, basic types, references and values ​​are stored on the stack.


2. Test is an object reference, which is stored in the stack, and the object (new Test()) is stored in the heap.
Test test = new Test();


3.test.change(date);
i is a local variable, and the reference and value are stored in the stack. When the method change is executed, i will disappear from the stack.


4.BirthDate d1 = newBirthDate(7,7,1970);


d1 is an object reference, which is stored in the stack, and the object (new BirthDate()) is stored in the heap, where d, m, and y are local variables stored in the stack, and their types are the basic types, so their data is also stored in the stack. middle. day, month, year are member variables, which are stored in the heap (in new BirthDate()). When the BirthDate constructor is executed, d, m, y will disappear from the stack.


5. After the main method is executed, the date variable, test, and d1 references will disappear from the stack, and new Test() and new BirthDate() will wait for garbage collection.


I hope that the introduction of the above content can help you.
Reprinted: http://news.newhua.com/news/2011/0802/128795.shtml

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327032552&siteId=291194637