(go) Summary of variable storage locations in Java

Source: Internet
Author: User

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: Store all new objects.
4. Static domain: Holds static members (statically defined)
5. Constant pool: Holds string constants and basic 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 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 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, 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.
such as the following code:

Java code
1.String S1 = "China";
2.String s2 = "China";
3.String s3 = "China";
4.String SS1 = new String ("China");
5.String SS2 = new String ("China");
6.String SS3 = new String ("China");
When a string is generated by new (assuming "China"), the constant pool is first searched for whether the "Chinese" object is already in use, and if none is created in the constant pool, then a copy object of this "China" object in the heap is created in a constant pool. This is the way of the interview: string s = new string ("xyz"), how many objects are produced? One or two, if there is no "xyz" in the constant pool, it is two.
For variables and constants of the underlying type: variables and references are stored in the stack, and constants are stored in the constant pool.
such as the following code:

Java code
1.int I1 = 9;
2.int i2 = 9;
3.int i3 = 9;
4.public static final int INT1 = 9;
5.public static final int INT2 = 9;
6.public static final int INT3 = 9;

For member variables and local variables: member variables are external to the method, variables defined internally by the class, and local variables are variables defined inside a method or statement block. Local variables must be initialized.
The formal parameter is a local variable, and the data for the local variable exists in the stack memory. Local variables in the stack memory disappear as the method disappears.
Member variables are stored in objects in the heap and are collected by the garbage collector.
such as the following code:

Java code
1.class BirthDate {
2. private int day;
3. private int month;
4. private int year;
5. Public BirthDate (int d, int m, int y) {
6. Day = D;
7. Month = m;
8. Year = y;
9.}
10. Omit the Get,set method ...
11.}
12.
13.public class test{
public static void Main (String args[]) {
15.int date = 9;
Test test = new test ();
Test.change (date);
BirthDate d1= New BirthDate (7,7,1970);
19.}
20.
. public void Change1 (int i) {
i = 1234;
23.}
For this code, date is a local variable, i,d,m,y is a local variable, and Day,month,year is a member variable. The following analysis of the code execution time changes:
1. The main method begins execution: int date = 9;
Date local variables, underlying types, references, and values are present in the stack.
2. Test test = new test ();
Test is an object reference, exists in the stack, and the object (new Test ()) exists in the heap.
3. Test.change (date);
I is a local variable, and the reference and value exist in the stack. When the method change execution is complete, I will disappear from the stack.
4. BirthDate d1= new BirthDate (7,7,1970);
D1 is an object reference, exists in the stack, the object (new BirthDate ()) exists in the heap, where D,m,y is stored in the stack for local variables, and their type is the underlying type, so their data is also stored in the stack. Day,month,year are member variables that are stored in the heap (new BirthDate ()). When the birthdate construction method finishes executing, the d,m,y disappears from the stack.
After the 5.main method executes, the date variable, TEST,D1 reference will disappear from the stack, new test (), new BirthDate () will wait for garbage collection.

(go) Summary of variable storage locations in Java

Related Article

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.