JVM variable storage location [Reserved]

1. Register: fastest storage area, performed by the compiler according to the demand assignment, we can not control in the program.
2. stacks: stack frame creation method when the method is performed, and the variable data stored basic types of objects referenced, but the object itself is not stored on the stack, but is stored in the stack (out of the new object) or a constant pool (constant string objects stored in the constant pool.)
3. the stack: store all new objects out.
4. A static field (Method Area): storing static member (static definition)
The constant pool (Method Area): storing basic types of string constants and constants (public static final).
6. Non-RAM storage: permanent hard disk storage space


where our main concern stack, heap and constant pool, and the object stack can be shared constant pool for the objects in the heap can not be shared. Data size and life cycle stack is for sure, when there are no references to the data, the data will disappear. Heap object is responsible for the garbage collector, and therefore does not need to determine the size and life cycle, with a lot of flexibility.
For strings: a reference to its objects are stored on the stack, if it is already compile created (defined directly by double quotation marks) is stored in the constant pool, if it is running on (new out) can be determined stored on the heap. For strings are equal equals, always only in a constant pool, multiple copies of the heap.
As the following 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");

 

Here to explain these three yellow arrow, for the generation of a string (assumed to be "china") through a new, looks to go to the pool for the constant has been "china" objects, if not create one in the constant pool this string object, and then re-create this copy heap objects "china" objects of a constant pool. This is the proper way to face questions: String s = new String ( " xyz"); produce several objects? One or two, if they had no "xyz" constant pool is two.

For the type of variable and constant basis: the stack, the constant pool stores constants and variables stored reference.

As the following 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 the member variables and local variables: internal member variable is defined externally methods, class variables; local variable is defined inside a method or a block statement variables. Local variables must be initialized.
Formal parameter is a local variable, local data variable on the stack memory. Local variable stack memory disappear with the disappearance method.
Member variables are stored inside the object heap, responsible for recycling by the garbage collector.

As the following code:

package com.study.stackheap;

/**
 * @author mdl
 * @date 2019/11/15
 */
public class BirthDate {
    private int day;  
    private int month;  
    private int year;      
    public BirthDate(int d, int m, int y) {  
        day = d;   
        month = m;   
        year = y;  
    }
    /**
     * @return the day
     */
    public int getDay() {
        return day;
    }
    /**
     * @param day the day to set
     */
    public void setDay(int day) {
        this.day = day;
    }
    /**
     * @return the month
     */
    public int getMonth() {
        return month;
    }
    /**
     * @param month the month to set
     */
    public void setMonth(int month) {
        this.month = month;
    }
    /**
     * @return the year
     */
    public int getYear() {
        return year;
    }
    /**
     * @param year the year to set
     */
    public void setYear(int year) {
        this.year = year;
    }  
    
    
}
package com.study.stackheap;

/**
 * @author mdl
 * @date 2019/11/15
 * https://blog.csdn.net/qq_34467922/article/details/80790443
 */

public class Test {
    public static void main(String args[]) {
        int date = 9;
        Test test = new Test();
        test.change(date);
        BirthDate d1 = new BirthDate(7, 7, 1970);
        System.out.println("date---" + date);
    }

    public void change(int i) {
        i = 1234;
    }
}

 

 

For the above code, date a local variable, i, d, m, y is a parameter for the local variables, day, month, year as a member variable. Analyze the following code execution time changes:
1. main method to begin: DATE = int. 9;
DATE local variables, base-type, and the reference value are present in the stack.
The Test Test new new = the Test 2. ();
Test object reference, the stack, the object (new Test ()) presence presence heap.
Test.change 3. (DATE);
I is a local variable, a reference value exists, and stack. After the completion of the implementation of the method change, i will disappear from the stack.
D1 = BirthDate BirthDate new new 4. (7,7,1970);  
D1 object reference, the stack, the object (new BirthDate ()) is present in the stack is present, where d, m, y for local variables stored on the stack, and type based on the type thereof, so their data is also stored on the stack. day, month, year as a member variable, they are stored on the heap (new BirthDate () inside). After the constructor BirthDate been executed, d, m, y disappears from the stack.
After 5.main complete execution method, date variable, test, d1 references from the stack disappear, new Test (), new BirthDate () will wait for garbage collection.

 

Guess you like

Origin www.cnblogs.com/bloodthirsty/p/11981017.html