Heap, stack, and constant pool in Java

Source: Internet
Author: User

1. Register

The fastest storage zone is allocated by the compiler as needed, which cannot be controlled 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 stored in the heap (new object) or constant pool (String constant objects are stored in the constant pool .)

3. Heap

Store all new objects.

4. Static domain

Static member Storage)

5. Constant pool

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

6. Non-RAM Storage

Permanent storage space such as hard disk

Here we mainly care about stacks, stacks, and constant pools. Objects in stacks and constant pools can be shared, but objects in stacks cannot be shared. The data size and lifecycle in the stack can be determined. When no reference points to the data, the data will disappear. The object in the heap is recycled by the garbage collector. Therefore, the size and lifecycle of the object do not need to be determined, so it has great flexibility.

For strings: the object references are stored in the stack. If a string has been created during the compilation period (defined in double quotation marks), it is stored in the constant pool, if it is confirmed during the runtime (new), it will be stored in the heap. For strings with equal equals, there is always only one copy in the constant pool, and there are multiple copies in the heap.

Run 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 basic types of variables and constants: variables and references are stored in the stack, and constants are stored in the constant pool.

Run 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: The member variables are the external methods, the variables defined inside the class, and the local variables are the variables defined inside the method or statement block. Local variables must be initialized. The formal parameter is a local variable, and the data of the local variable is stored in the stack memory. The local variables in the stack memory disappear as the method disappears.

The member variables are stored in the heap object and are recycled by the garbage collector.

Run 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 ;} omit get, set method ...... } Publicclass Test {publicstaticvoid main (String args []) {int date = 9; Test test = new Test (); test. change (date); BirthDate d1 = new BirthDate (1970, 1234);} publicvoid change1 (int I) {I =;

For the above Code, date is a local variable, and I, d, m, and y are all member variables with the form parameters as local variables, day, month, and year. The following describes the changes in code execution:

1. Run the main method:

Int date = 9;

Date local variables, basic types, references, and values all exist in the stack.

2. test is an object reference. It exists in the stack and the object (new Test () exists in the heap.

Test test = new Test ();

3. test. change (date );

I is a local variable and the reference and value exist in the stack. After the method change is executed, I disappears from the stack.

4. BirthDate d1 = new BirthDate (1970 );

D1 is an object reference and is in the stack. The object (new BirthDate () is 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. Day, month, and year are member variables, which are stored in the heap (new BirthDate ). After the BirthDate constructor is executed, d, m, and y will disappear from the stack.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.