1.Register: The fastest storage area, allocated by the compiler on demand,we have no control over 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 (Newobject) or in a constant pool (a string constant object is stored in a constant pool. )
3.Heap: Store allNewout of the object.
4.static domain: holds static members (Staticdefined)
5.constant Pool: holds string constants and basic type Constants (Public static Final).
6.Non-RAMStorage: Persistent storage space such as hard drives
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 objects are stored in the stack, if the compilation period has been created.(defined directly in double quotation marks.)is stored in a constant pool, if it is a run-time (Newstored in the heap to be determined. Forequalsequal strings, which are always only one copy in a constant pool, with multiple copies in the heap.
such as the following code:
String S1= ' China ' = ' China '= 'China 'new string ("China"new String ("China",new string ("China");
for passing new produces a string (assuming "China" string s = new string ("xyz"); How many objects are produced? One or two if the constant pool originally did not have is two.  
for variables and constants of the underlying type: variables and references are stored in the stack and constants are stored in a constant pool.  
such as the following code:  
int i1 = 9; int i2 = 9= 9; publicstaticfinalint INT1 = 9; publicstaticfinalint INT2 = 9; publicstaticfinalint
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:
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 voidChangeinti) {i= 1234; }
For this piece of code,Dateis a local variable,I,d,m,yare all parameters as local variables, Day,Month, Yearas a member variable. The following analysis of the code execution time changes:
1. Mainmethod to start execution:int date = 9;
Datelocal variables, underlying types, references, and values are present in the stack.
2. Test test = new test ();
Testfor object reference, exists on stack, object(New Test ())exists in the heap.
3. Test.change (date);
Ifor local variables, references and values exist in the stack. When the method Changewhen execution is complete,Iwill disappear from the stack.
4. BirthDate d1= new BirthDate (7,7,1970);
D1for object reference, exists on stack, object(New BirthDate ())exists in the heap, whereD,m,ylocal variables are stored in the stack, and their type is the underlying type, so their data is also stored in the stack. Day,month,yearas member variables, which are stored in the heap(New BirthDate ()inside). WhenBirthDatewhen the construction method finishes executing,D,m,ywill disappear from the stack.
5.mainwhen the method finishes executing,Datevariables,Test,D1references will disappear from the stack,new Test (), New BirthDate ()will wait for garbage collection.
Where variables are stored in Java