Analysis of the difference between heap and stack in Java

  • 2020-04-01 03:27:53
  • OfStack

Heap and stack are very important concepts in Java data structures, and this article analyzes the differences between them in more detail. For your reference. The details are as follows:

The Java heap is a runtime data area from which a class (object) allocates space. These objects are created by instructions such as new, newarray, anewarray, and multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, and the advantage of the heap is that you can dynamically allocate the memory size, and you don't have to tell the compiler in advance about the lifetime, because it dynamically allocates memory at run time, and the Java garbage collector automatically collects the data that is no longer used. The disadvantage is that access is slow because memory is allocated dynamically at run time.

The advantage of the stack is that access is faster than the heap, second only to registers, and stack data can be Shared. The disadvantage is that the size and lifetime of the data in the stack must be fixed and there is no flexibility. The stack mainly contains variables of basic types (int, short, long, byte, float, double, Boolean, char) and object handles.

An important peculiarity of the stack is that the data stored in the stack can be Shared. Suppose we define:
Int a = 3;
Int b = 3;
The compiler first processes int a = 3; It will first create a reference to the variable a in the stack, then look for the value 3 in the stack, if it doesn't find it, store the 3 in, and then point a to 3. And then int b = 3; After creating the reference variable for b, point b directly to 3 because there is already a value of 3 on the stack. So you have a situation where both a and b are pointing to 3 at the same time.

Now, if I set a equal to 4; Then the compiler will search the stack again to see if there are 4 values. If not, it will store 4 in the stack and make a point to 4. If you already have it, point a directly to the address. So the change in a doesn't affect b.

Note that this sharing of data is different from the sharing of references to two objects pointing to one object at the same time, because in this case the modification of a does not affect b, it is done by the compiler, and it helps save space. When an object reference variable changes the internal state of that object, it affects another object reference variable.

String is a special wrapper class data. You can use:


String str = new String("abc");
String str = "abc";

There are two ways to create it. The first is to create the object with new(), which is stored in the heap. Each call creates a new object.

The second way is to first create an object in the stack to refer to the String class STR variable, and then find out whether there is "ABC" in the stack. If there is no "ABC", then store "ABC" on the stack, and make STR refer to "ABC", if there is "ABC", then directly make STR refer to "ABC".

The equals() method is used when comparing values in a class. When testing whether references to two wrapper classes point to the same object, use == to illustrate the above theory with an example.


String str1 = "abc"; 
String str2 = "abc"; 
System.out.println(str1==str2); //true

You can see that str1 and str2 are pointing to the same object.


String str1 =new String ("abc"); 
String str2 =new String ("abc"); 
System.out.println(str1==str2); // false

The way to do new is to generate different objects. One at a time.

Therefore, creating multiple "ABC" strings in the first way only means that there is only one object in memory. For String STR = new String(" ABC "); , which creates new objects in the heap regardless of whether their string values are the same or whether it is necessary to create new objects, thus increasing the burden on the program.

On the other hand, note that we are using things like String STR = "ABC"; When you define a class, you always take it for granted that the object STR of the String class is created. Worry about the trap! The object may not have been created! It may simply point to an object that has been created previously. Only the new() method guarantees that a new object is created each time.

Because of the immutable nature of the String class, the StringBuffer class should be considered to improve program efficiency when the String variable needs to constantly change its value.

I hope this article will be helpful to your Java programming study.


Related articles: