Reference: http://blog.csdn.net/miraclestar/article/details/6039743
1. Register: The fastest storage area, allocated by the compiler on demand, is beyond our 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 (new Object) or in a constant pool (the string constant object is stored in a constant pool) .
3. heap : Store all new out of the object .
4. Static domain : holds static members (statically defined)
Span style= "font-family: ' Microsoft Yahei ';" > 5. : Storage and (public static final). &NBSP
6. Non-RAM storage : Permanent storage space such as hard disk
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.
Part 1
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.
Java code
String S1 = "China"; = "China"; = "China"; New String ("China"); New String ("China"); New
This explains the yellow 3 arrows, for the creation of a string through new (assuming "China"), will first go to the constant pool to find whether the "China" object, if not a constant pool to create this string object, and then create a constant pool in the heap in this " The copy object of the "China" object.
Youdao Interview Question: 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.
Part 2
for : variable and reference stored in Stack , constant (final) stored in in .
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
Part 3
For member variables and local variables: Local variables are on the stack , and member variables are in the heap.
A member variable is a variable that is internally defined by a method , or a variable that is defined inside a method or statement block. Local variables must be initialized.
member variables are stored in objects in the heap and are collected by the garbage collector.
Local variables exist in the stack memory. Local variables in the stack memory disappear as the method disappears. Formal parameters are local variables
Local variables must be initialized, member variables can not be initialized
class myclass{ public int TA; public void Printta () { int temp; // system.out.println (temp); // error, local variable must be initialized System.out.println (TA); //
Comprehensive Example:
classBirthDate {Private intDay ; Private intmonth; Private intYear ; PublicBirthDate (intDintMinty) { day=D; Month=m; year=y; } //omit the Get,set method ...} Public classtest{ Public Static voidMain (String args[]) {intDate = 9; Test test=NewTest (); Test.change (date); BirthDate D1=NewBirthDate (7,7,1970); } Public voidChange1 (inti) {i= 1234; } }
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.
Java Learning notes-heap, stack, Chang