Preface
I wrote an article about the division of JVM memory area, but yesterday I received an interview with Ant Financial. I asked about JVM related content and explained the division of JVM memory area. This part was pretty good, but later I asked where String is stored in Java. I only remembered that String is an unchanged quantity, which should be stored in the constant pool. But later I asked where new String should be placed, which should be placed in the heap. Later, I asked where String references were placed. At that time, I was foolish and said that they were also placed in the heap. Now I will summarize: Basic type variable data and object references are placed in the stack, the object itself is placed in the heap, explicit String constants are placed in the constant pool, and String objects are placed in the heap.
Description of constant pool
The constant pool was previously placed in the method area, that is, it was in the permanent generation, and moved to the heap starting from JDK7. This change can be seen from the ** Important RFEs Addressed in JDK 7 notes in the release version of oracle.
Area: HotSpot Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the () method will see more significant differences. RFE: 6962931
String memory location description
1. Explicit String constants
String a = "holten"; String b = "holten";
•After the first sentence, a String object with the value holten is created in the constant pool;
•When the second sentence is executed, because there is a holten in the constant pool, a new String object is no longer created.
•At this time, the reference to the string is in the virtual machine stack.
Object
String a = new String("holtenObj"); String b = new String("holtenObj");
•When Class is loaded, a String object with the value of holtenObj is created in the constant pool. When the first sentence is executed, a new String("holtenObj") object will be created in the heap;
•When the second sentence is executed, because holtenObj exists in the constant pool, a new String object is no longer created, and a new String("holtenObj") object is directly created in the heap.
Verify
/** * Created by on 2016/8/16. */ public class Main { public static void main(String[] args){ String str1 = "Gao Xiaotian"; String str2 = "Gao Xiaotian"; (str1==str2);//true String str3 = new String("Tao Datian"); String str4 = new String("Tao Datian"); (str3==str4);//false } }
Return result:
true false
The above article is based on the detailed explanation of string memory locations in Java. It is all the content I share with you. I hope you can give you a reference and I hope you can support me more.