Heap and stack are very important concepts in Java data structure. This article analyzes the difference between the two in more detail. For reference. The details are as follows:
Java's heap is a runtime data area, from which class (objects allocate space. These objects are created through instructions such as new, newarray, anewarray, and multianewarray. They do not require program code to explicitly Release. The heap is responsible for garbage collection. The advantage of the heap is that it can dynamically allocate memory size, and the lifetime does not need to be told to the compiler in advance, because it allocates memory dynamically at runtime, and Java's garbage collector will automatically collect it. The disadvantage is that due to the dynamic allocation of memory at runtime, the access speed is slower.
The advantage of the stack is that the access speed is faster than the heap, second only to the register. , stack data can be shared. But the disadvantage is that the size and lifetime of the data stored in the stack must be determined, and there is a lack of flexibility. The stack mainly stores some basic types of variables (int, short, long, byte, float, double). , boolean, char) and object handles.
The stack has a very important special feature, that is, the data stored in the stack can be shared. Suppose we define at the same time:
int a = 3;
int b = 3;
The compiler first processes int a = 3; first it will create a reference to the variable a in the stack, and then search whether there is a value of 3 in the stack. If not found, it will store 3 in. Then point a to 3. Then process int b = 3; after creating the reference variable of b, since there is already a value of 3 on the stack, b will be pointed directly to 3. In this way, a and b will both point to 3. In the case of 3.
At this time, if a=4 is set again; then the compiler will re-search whether there is a 4 value in the stack. If not, it will store 4 in and make a point to 4; if it is already If so, point a directly to this address. Therefore, changes in the value of a will not affect the value of b.
It should be noted that this kind of data sharing and the reference of two objects point to one object at the same time. Sharing is different, because in this case the modification of a does not affect b, it is done by the compiler, which helps save space, and if an object reference variable modifies the internal state of the object, it will affect another. An object reference variable.
String is a special wrapper class data. It can be created in two forms:
String str = new String("abc"); String str = "abc";
#. One is to use new() to create a new object, which will be stored in the heap. And the second is to first create a String on the stack. The object of the class refers to the variable str, and then checks whether "abc" is stored in the stack. If not, store "abc" on the stack and make str point to "abc". If there is already "abc", directly make str point to " abc". When comparing whether the values in the class are equal, use the equals() method; when testing whether the references of two packaging classes point to the same object, use ==. The following example illustrates the above theory. .
String str1 = "abc"; String str2 = "abc"; System.out.println(str1==str2); //true
String str1 =new String ("abc"); String str2 =new String ("abc"); System.out.println(str1==str2); // false