1. There is such a statement, now competing in the IT battlefield of the two major forces, Ms Family focus on the bottom of the implementation of the Java family focus on the system architecture. According to no textual research, but from the two major forces of their respective community strength and book market has a masterpiece is not difficult to see, this is not true, but Master Java's low-level implementation of Java programmers is critical, this article describes the Java data in memory storage.
2 in-memory heap (stack) and stack (heap)
Java programs run with 6 places to store data, which are registers, stacks, heaps, static storage, constant storage, and non-RAM storage, mainly heap and stack storage.
"Random Memory: Random Access Memory"
Stacks and heaps are places where Java is used to store data in RAM. Unlike C + +, Java automatically manages stacks and heaps, and programmers cannot directly set stacks or heaps. The advantage of the stack is that the access rate is faster than the heap, second only to the registers directly located in the CPU. In addition, stack data can be shared. The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. The advantage of the heap is that the memory size can be dynamically allocated, and that the Java garbage collector automatically collects the data that is no longer in use without having to tell the compiler beforehand. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time.
"Register is in CPU"
3 in-memory storage of data in Java
3.1 Storage of basic data types
Java has a total of 8 basic data types, i.e. int, short, long, byte, float, double, Boolean, char (note that there is no basic type of string). This type of definition is defined by a form such as int a = 3; long B = 255L, called an automatic variable. It is noteworthy that the automatic variable is a literal value, not an instance of a class, that is, not a reference to a class, where there is no class. such as int a = 3; Here's A is a reference to the int type, pointing to the 3 literal. These literal data, due to the size of known, lifetime known (these literals are fixed in a program block, the program block exit, the field value disappears), for the pursuit of speed reasons, exist in the stack.
In addition, the stack has a very important particularity, is the existence of the stack of data can be shared. Let's say we both define:
int a = 3; int b=3;
The compiler first deals with int a = 3; First, it creates a reference to a variable in the stack, and then looks for an address with a literal value of 3, then opens an address that holds the face value of 3 and then points a to 3. then the int b = 3 is processed, and after the B-reference variable is created, the B is pointed directly to the address of 3 because it already has 3 of the literal value on the stack. In this way, there is a case where A and B both point to 3. "Reference + value + memory address" mentioned above three nouns, in which the variable name is a reference, the value assigned to the variable is a number,
And the memory mentioned is abstract, so that the reference is not a numeric value, but a memory address that accesses the value.
After defining the value of a and B, then make a = 4; then, b is not equal to 4, or equal to 3. Inside the compiler, when encountered, it will search the stack for 4 literal values, if not, reopen the address to store the value of 4, and if so, point a directly to the address. Therefore the change of a value does not affect the value of B.
"Define a variable, assign a value to a variable, and then save it in memory during compilation."
Memory model for 3.2 objects
In Java, create an object that includes the declaration and instantiation of an object, using an example to illustrate the object's memory model.
Suppose that a class rectangle is defined as follows: "Rectangle: Rectangular】
Class rectangle{
Double width,height;
Rectangle (Double w,double h) {
WIDTH=W;HEIGHT=H}}
(1) The memory model when declaring an object
When an object rect is declared with rectangle rect, the memory space is allocated for the reference variable rect the object in stack memory, but the rectangle value is empty, and the rect is an empty object. A null object cannot be used because it does not yet reference any "entity".
(2) Memory model for object instantiation
When executing rect=new Rectangle (3,5), two things are done:
Allocates memory for the class's member variable Width,height in heap memory and initializes it to the default value for each data type, followed by an explicit initialization (the initialization value when the class is defined), and the last call to the constructor method to assign a value to the member variable.
A reference to the object in the heap memory (equivalent to the first address) is rect to the reference variable, and the object in the heap memory can then be referenced by Rect.
(3) Create multiple different object instances
A class can create several different object instances by using the new operator, which will be allocated different memory spaces in the heap, and changing the state of one of the objects will not affect the state of other objects. For example:
Rectangle r1=new Rectangle (3,5);
Rectangle r2=new Rectangle (4,6);
At this point, the memory space is allocated to the member variable width and height of two objects in heap memory, and the space occupied by the two objects in the heap memory is different from each other. If there is
Rectangle r1=new Rectangle (3,5);
Rectangle R2=r1;
Only one object instance is created in heap memory, two object references are created in stack memory, and two object references point to an object instance.
3.3 Storage of packing class data
Basic types have corresponding packaging classes: such as int corresponding to the integer, double double, and so on, the basic type of definition is directly in the stack, if you use the wrapper class to create objects, just like ordinary objects. For example: int i=0;i is stored directly in the stack. Integer I (I at this time is an object) = new Integer (5); Thus, the I object data is stored in the heap, the reference to I is stored on the stack, and the object is manipulated by reference in the stack. "Data is stored in the heap, references are stored in the stack"
3.4 The storage of String type data
A string is a special wrapper class data. Can be created in the form of string str = new String ("abc");
Can also be created in the form of String str = "ABC";
The first way to create a normal object is the same as the creation process;
The second way to create it is to translate this statement into the following steps within Java:
(1) First define an object reference variable named str to the String class: String str;
(2) in the stack to find there is no store value of "ABC" address, if not, create an address that holds the value of "ABC", then creates an object o of the new string class, points to the address of the string value of O, and notes the object o of the reference next to this address in the stack. If you already have an address with the value "ABC", look for the object o and return the address of O.
(3) Point Str to the address of object o.
It is noteworthy that the string values in the generic string class are stored directly. But like string str = "abc"; in this case, its string value holds a reference to the data in the existing stack.
To better illustrate this issue, we can verify this by using several of the following code.
String str1= "ABC";
String str2= "ABC";
System.out.println (S1==S2);//true
Note that str1.equals (STR2) is not used here, as this will compare the values of two strings for equality. The = = number, as described in the JDK, returns the true value only if two references are pointing to the same object. And what we're looking at here is whether str1 and str2 all point to the same object.
Let's look at the following code again.
Stringstr1=new String ("abc");
stringstr2= "ABC";
System.out.println (STR1==STR2);//false
Two references were created. Two objects were created. Two references point to different two objects.
The above two pieces of code show that as long as the new object is created with new (), it is built in the heap and its string is stored separately, even if it is the same as the data in the stack, and is not shared with the data in the stack.
memory allocations for 3.5 arrays
When an array is defined, int x[], or int []x, an array reference is created in stack memory to refer to the array through that reference (that is, the array name). X=new int[3]; 3 spaces in heap memory that hold int data, the first address of heap memory is placed in the stack memory, and each array element is initialized to 0.
4 Release of memory space
The lifetime of the variables on the stack is limited to the lifetime of the current function, the function exits, and the variable does not exist. An object instance that is allocated in the heap that can be reclaimed by the garbage collection mechanism when no longer has any reference variable pointed to it.
5 Summary Stack then look at Java memory, stack memory to hold some of the basic types of variables and arrays and object reference variables, and heap memory is mainly to drop object instances. Understanding this will be a good way to explain the problem of polymorphism, inheritance, and coverage.