In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "JVM string constant pool and String intern method example analysis". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "JVM string constant pool and String intern method example analysis" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of Xiaobian.
interview questions
First, let's understand the presentation form of the content we are going to talk about in this article through an interview question image:
String s1 = new String("he") + new String("llo");String s2 = new String("h") + new String("ello");String s3 = s1.intern();String s4 = s2.intern();System.out.println(s1 == s3);System.out.println(s1 == s4);
Executing the above code will find that the printed result is true. So, why are strings that are not equal equal after calling the intern method? Let's take a look at the underlying implementation step by step.
The role of intern methods
Definition of function of intern() method:
(1) If the current string content exists in the string constant pool (that is, the equals() method is true, that is, the content is the same), then directly return the reference of this string in the constant pool;
(2) If the current string is not in the string constant pool, create a reference in the constant pool pointing to a string already in the heap, and return the reference in the constant pool.
Simply put, intern method is to determine whether the string exists in the string constant pool, if it does not exist, create it, and if it exists, return it.
String constant pool
The string constant pool implemented in HotSpot is a StringTable class, which is a Hash table with a default size of 1009. There is only one copy in each instance of HotSpot VM, shared by all classes. String constants consist of individual characters placed on StringTable.
JDK6 and earlier, string constant pools are placed in the Perm Gen section (Methods section). StringTable length is fixed, the length is 1009, when too many String strings will cause hash conflict, resulting in long list, performance greatly reduced. In this case, all string constants in the string constant pool are string constants (literal values).
Because of the limited and fixed space for permanent generations, JDK6's storage mode is prone to OutOfMemoryError.
JDK7 was working on permanent generation, so the string constant pool was placed in the heap. In this case, even though the heap size is fixed, only the heap size needs to be adjusted for application tuning.
In JDK7, string constant pools can store not only string constants, but also string references. That is, references to strings in the heap can exist as constant pool values.
String Pooling Process Analysis
After understanding the basic theory above, we will demonstrate the process and classification of string pooling step by step in the form of a combination of pictures and texts. The following examples are based on JDK8.
When we declare a string in double quotes:
String wechat = "new horizon of program";
In this case, the string enclosed in double quotes is stored directly in the string constant pool.
As for the storage structure above, we have already mentioned it in previous articles, so we will not explain it too much. Let's see what happens if we declare the same string again.
String wechat = "Program new horizon";String wechat1 = "Program new horizon";
When wechat1 is declared in the above code, it will be found that the corresponding string already exists in the constant pool, and it will not be recreated, but the corresponding reference will be returned to wechat1.
In this case, wechat and wechat1 must be equal if we compare them directly with the double equal sign, because their references and literals are the same.
The above is the case of direct double-quote assignment, so what if the string is created in the form of new? As mentioned in the previous article, there are two kinds of cases: the existence of corresponding values in the constant pool and the absence of corresponding values.
String wechat2 = new String("Program new horizon");
If there is a corresponding value, an object reference to the wechat2 variable is created in the heap, and then the object reference is pointed to a constant that already exists in the string constant pool.
In this case, directly using the double equal sign to compare wechat and wechat2 variables is definitely not equal, while comparing literal values through the equals method is equal.
Another case is when a string constant is created with new and there is no corresponding constant in the string constant pool. This creates a string constant in the string constant pool, and then creates a string in the heap that holds a reference to the corresponding string in the constant pool. And return the address of the heap object to wechat2. The final result is still as shown above.
At this point, the situation is different if you don't assign new directly, but through the + sign operation.
String s1 = "program";String wechat3 = new String(s1 + "new horizon");
The above code s1 will be stored in the constant pool, while the value of wechat3 will only create a String object in the heap and will not store the corresponding string in the constant pool because the JVM compiles with StringBuilder for plus concatenation.
The situation at this point has already involved the creation of strings in our interview questions. Then, let's pool through intern method to see the specific changes of string constant pool.
Also take the above code as an example, at this time wechat, wechat1, wechat2 three variables and wechat3 directly with double equal sign comparison is definitely not equal. Let's intern pool wechat3.
String s1 = "program";String wechat3 = new String(s1 + "new horizon");wechat3 = wechat3.intern();
At this point, you will find that the two variables wechat and wechat1 are equal to the values of wechat3. Since wechat and wechat1 are actually one, here is only a comparison of wechat and wechat3 as an example to analyze this process.
The state of memory before the invocation of the intern method is something like the following (omitting the s1 part)
It's not surprising that their values are not equal. Let's pool wechat3 and assign the pooled result to wechat3, which is the code above. The memory structure changes as follows
At this point, the corresponding two values are judged again, because the reference and literal values are all the same, so they are equal. We already know the specific intern judgment rules above. If there is a corresponding value in the constant pool, the reference is returned directly.
Then there is another situation, that is, there is no corresponding value in the constant pool, how to deal with it? First look at the following code:
String s2 = "follow";String wechat4 = new String(s2 + "public number");wechat4 = wechat4.intern();
The operation before calling intern, as we said earlier, creates a String object in the heap, and does not store a copy in the constant pool, just like the graph in wechat3.
At this time, there is no corresponding string in the constant pool. After calling the intern method,
After the intern method, the constant pool contains references to the corresponding strings in the heap. In contrast to the above, JDK7 and later string constant pools can store references.
It should be noted that when there is no corresponding string in the string constant pool, the address returned by calling the intern method is the address in the heap, corresponding to 0x99 in the figure. The wechat4 address points to the address in the heap, so it will not change.
At this point, if you define a double quotation mark assigned wechat5, the following code:
String s2 = "Attention";String wechat4 = new String(s2 + "Public Number");wechat4 = wechat4.intern();String wechat5 = "Attention Public Number";System.out.println(wechat4 == wechat5);
When the variable wechat5 is initialized, it is found that there is already a reference in the string constant pool, so wechat5 will directly point to this reference, that is, wechat5 and wechat4 point to the String object in memory.
summary
The main point to note in the above demo is the reference address returned by the intern method. If there is already a corresponding string in the string constant pool, the address of the string constant is returned [string stored in the constant pool]. If there is no corresponding string in the string constant pool, the reference in the heap will be placed in the corresponding position of the constant pool [string reference stored in the heap]. At this time, intern returns the reference corresponding to the string in the heap.
Check out the return logic above and look at the original code:
String s1 = new String("he") + new String("llo");String s2 = new String("h") + new String("ello");String s3 = s1.intern();String s4 = s2.intern();System.out.println(s1 == s3);System.out.println(s1 == s4);
where s1 is the address of the string "hello" in the heap;s2 is the address of another string "hello" in the heap. When s1.intern() stores the address of s1 in the constant pool, s1.intern() returns the address of s1, so s1=s3, which is the same address.
Then execute s2.intern(). At this time, there is already a hello string in the constant pool, which is of type reference and points to the address of s1. After execution, the address returned is the address of s1, which is assigned to s4, so s1 and s4 also point to the same address, so they are equal.
Through the above in-depth analysis, we must have a deeper understanding of string constants, string constant pools, and intern methods. If the relevant interview questions are analyzed according to this idea, they can basically be answered accurately.
Read here, this article "JVM string constant pool and String intern method example analysis" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more about the content of the article, welcome to pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.