Memory Analysis
1. The types of variables in the Java language, except for the basic types, are called application types. Objects in Java are manipulated by reference.
2. The distinction between classes and objects in memory:
A, class is a static concept, stored in the code area;
B, the object is new out, in the heap memory (dynamic), the class of each member variable in different objects have different values (in addition to static variables), and the method is only one, the execution of the time to occupy memory.
3, Java in memory allocation of areas involved:
Registers: We have no control in the program
Stacks: A reference to the underlying type of data and objects, but the object itself is stored in the heap instead of being stored on the stack
Heap: Storing data produced with new
Static fields: Static members that are stored in the object with static definitions
Constant pools: Storing constants
Non-RAM storage: Permanent storage space such as hard disk
4, memory example analysis:
A, main class:
Package yc.test;
public class T {public
static void Main (string[] args) {
test test = new test ();
Test.seti (2);
TEST.SETJ (2);
System.out.println ("Add:" +test.add ());
}
B, Auxiliary class:
Package yc.test;
public class Test {
private int i = 0;
private int j = 0;
public int Add () {return
i+j;
}
public int Geti () {return
i;
}
public void SetI (int x) {
this.i = x;
}
public int Getj () {return
J;
}
public void setj (int y) {
this.j = y;
}
}
Analysis: Starting from the main function,
① in stack memory, create a reference variable test and set the value to null;
② in heap memory, generates an object test and assigns its space address value to the reference variable test;
③ in stack memory, create a basic data type variable x and assign a value of 2;
④ assigns the I (heap memory) of the object in test to X;
After the ⑤ completes the assignment, the x variable in the stack memory is released before the exit method returns to the main function.
⑥ Repeat ③④⑤ step to complete the assignment of J of the object referred to in test;
⑦test calls the Add method to read the value of I and J in the heap memory and completes the output.