Heap and stack are very important concepts in Java data structure. This article analyzes the difference between the two in more detail. For your 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, newaray, anewarray, and multianewarray, and they do not require program code to be explicitly released. The heap is collected by garbage collection Responsible, 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 dynamically allocates memory at runtime, and Java's garbage collector will automatically collect these no longer used data. But the disadvantage is that due to the need to dynamically allocate memory at runtime, the access speed is slow.
The advantage of the stack is that the access speed is faster than the heap, second only to the register, and the 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.
A very important special feature of the stack is that the data stored in the stack can be shared. Suppose we also define:
int a = 3;
int b = 3;
The compiler first processes int a = 3; first it creates a reference to the variable a on the stack, and then checks whether there is a value of 3 on the stack. If not found, it stores 3 in, and then points a to 3. Then process int b = 3; after creating the reference variable of b, because there is already a value of 3 on the stack, b will be pointed directly to 3. In this way, there is a situation where a and b both point to 3 at the same time.
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 already exists, it will directly point a 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 is different from the sharing of two objects' references pointing to one object at the same time, because in this case the modification of a will not affect b, it is completed by the compiler, which is beneficial to Save space. If an object reference variable modifies the internal state of the object, it will affect another object reference variable.
String is a special wrapper type of data. Can be used:
String str = new String("abc");String str = "abc";
There are two ways to create it. The first one is to use new() to create a new object, which will be stored in the heap. A new object is created each time it is called.
The second method is to first create an object reference variable str of the String class on the stack, and then check 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 point str to "abc".
When comparing whether the values in a class are equal, use the equals() method; when testing whether the references of two wrapper 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
It can be seen that str1 and str2 point to the same object.
String str1 =new String ("abc"); String str2 =new String ("abc"); System.out.println(str1==str2); // false
Using new is to generate different objects. Generate one at a time.
Therefore, if you use the first method to create multiple "abc" strings, there is actually only one object in the memory. This way of writing is beneficial to saving memory space. At the same time, it can improve the running speed of the program to a certain extent, because the JVM will Automatically determine whether it is necessary to create a new object based on the actual situation of the data in the stack. For the code of String str = new String("abc");, new objects are always created in the heap, regardless of whether their string values are equal or whether it is necessary to create new objects, thus increasing the burden on the program.
On the other hand, please note: When we define a class using a format such as String str = "abc";, we always assume that the object str of the String class is created. Worry about traps! The object may not have been created! Instead it may just point to a previously created object. Only through the new() method can we ensure that a new object is created every time.
Due to the immutable nature of the String class, when a String variable needs to frequently change its value, you should consider using the StringBuffer class to improve program efficiency.
I hope that what this article describes will be helpful to everyone's learning of Java programming.