Heap, stack, and constant pools in Java

Source: Internet
Author: User
Tags constant final garbage collection

1. Registers

The fastest storage area, which is allocated by the compiler on demand, is beyond our control in the program.

2. Stack

A reference to a variable data and an object that holds the base type, but the object itself is not stored on 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

Store all the new objects.

4. Static domain

Holds static members (defined by static)

5. Constant Pool

Holds string constants and base type constants (public static final).

6. Non-RAM storage

Permanent storage space such as hard disk

Here we are primarily concerned with stacks, heaps, and constant pools, which can be shared for objects in stacks and constant pools, and not for objects in the heap. The data size and lifecycle in the stack can be determined, and the data disappears when no reference is directed to the data. The objects in the heap are reclaimed by the garbage collector, so the size and lifecycle do not need to be determined and have great flexibility.

For strings: The references to their 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 and stored in the heap if the runtime (new) is established. For equals equal strings, there is always only one copy in the constant pool, with multiple copies in the heap.

such as the following code:

String S1 = "the"; String s2 = "the"; String s3 = "the"; String ss1 = new String ("a"); String ss2 = new String ("a"); String ss3 = new String ("a");

For variables and constants of the underlying type: variables and references are stored on the stack, and constants are stored in constant pools.

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

The member variable is stored in the object in the heap and is reclaimed 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; mont h = 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 (7,7,1970); } publicvoid change1 (int i) {i = 1234;

For the above code, date is a local variable, i,d,m,y is a local variable, and Day,month,year is a member variable. The following is an analysis of the changes in code execution:

1. The main method begins execution:

int date = 9;

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

2. Test is an object reference, where the object (new Test ()) exists in the heap.

Test test = new test ();

3.test.change (date);

I is a local variable, and references and values exist in the stack. When the method change execution completes, I will disappear from the stack.

4.BirthDate d1= New Birthdate (7,7,1970);

D1 is a reference to an object, where the object (new Birthdate ()) exists in the heap, where D,m,y is stored on the stack for local variables, and their types are underlying types, so their data is also stored on the stack. Day,month,year are member variables that are stored in the heap (new Birthdate ()). When the birthdate construction method is finished, the d,m,y disappears from the stack.

After the 5.main method finishes, the date variable, the TEST,D1 reference disappears from the stack, new test (), and the new birthdate () waits 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.