In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces JVM string constant pool and String intern method, the content is very detailed, interested friends can refer to, hope to be helpful to you.
The comparison of strings has been explained in detail in the previous article, and this article provides a further explanation based on the storage of the string constant pool and the memory changes caused by using the intern method.
Key content: what happens when the string is compared after the string calls the intern method?
Interview questions
First of all, take a look at the presentation form of the content we are going to talk about in this article through an interview question:
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)
Execute the above code and you will find that the printed results are all true. So why are strings that are not equal equal when the intern method is called? Let's analyze the underlying implementation step by step.
The function of intern method
The functional definition of the intern () method:
(1) if the content of the current string exists in the string constant pool (that is, the equals () method is true, that is, the content is the same), the reference of the string in the constant pool is directly returned.
(2) if the current string is not in the string constant pool, create a reference in the constant pool and point to a string that already exists in the heap, and then return the reference in the constant pool.
To put it simply, the intern method determines whether the string exists in the string constant pool, creates it if it does not exist, and returns if it does.
String constant pool
What implements the string constant pool function in HotSpot is a StringTable class, which is an Hash table with a default size length of 1009. There is only one instance of each HotSpot virtual machine, which is shared by all classes. String constants are made up of characters and are placed on the StringTable.
In "interview questions Series 5: JDK running constant pool, string constant pool, static constant pool, but also foolishly confused?" In this article, we have specifically described that the location of the string constant pool varies with the JDK version, which can be used for reference.
In JDK6 and previous versions, the string constant pool is placed in the Perm Gen area (method area). The length of StringTable is fixed, and the length is 1009. When there are too many String strings, it will cause hash conflicts, resulting in long linked lists and significant performance degradation. At this point, all the string constants (literals) are placed in the string constant pool.
Due to the limited and fixed space of the permanent generation, the storage mode of JDK6 can easily lead to OutOfMemoryError.
JDK7 is working on permanent generation, so the string constant pool is placed in the heap. At this point, even the heap size is fixed, but for application tuning, you only need to adjust the heap size.
In JDK7, string constant pools can hold not only string constants, but also references to strings. That is, a reference to a string in the heap can exist as the value of a constant pool.
Analysis of string pooling process
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 text. The following example is analyzed and explained based on the JDK8 version.
When we declare a string in double quotes:
String wechat = "New Horizon of Program"
At this point, the string within the double quotation marks is stored directly in the string constant pool.
With regard to the above storage structure, we have already mentioned in the previous article, we will not explain too much. Let's see what happens if we declare the same string again.
String wechat = "New Vision of Program"; String wechat1 = "New Vision of Program"
When you declare wechat1 in the above code, you will find that the corresponding string already exists in the constant pool, so it will not be recreated, but the corresponding reference will be returned to wechat1. The corresponding structure diagram is as follows:
At this point, if you compare wechat and wechat1 directly with double equal signs, they must be equal, because their references and literal values are the same.
The above is the case of direct double quotation mark assignment, so what about the process of creating a string in the form of new? As mentioned in the previous article, there are two cases: there is a corresponding value in the constant pool and there is no corresponding value.
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.
At this point, it is certainly not equal to compare wechat and wechat2 variables directly with double equal signs, while comparing literal values through the equals method is equal.
Another situation is that when created through new, there is no corresponding constant in the string constant pool. This situation now 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 object in the heap to wechat2. The final effect picture is still like the above picture.
At this point, the situation is different if the assignment is not a direct new string assignment, but is operated by the + sign.
String S1 = "Program"; String wechat3 = new String (S1 + "New Horizon")
The above code S1 is stored in the constant pool, while the value of wechat3 is compiled with StringBuilder to concatenate the plus sign, so only a String object is created in the heap and the corresponding string is not stored in the constant pool.
The situation at this time has already involved the situation of creating strings in our interview questions. So, let's do the pooling operation through the intern method to see the specific changes in the string constant pool.
Also take the above code as an example, at this time the three variables wechat, wechat1, wechat2 and wechat3 directly with the double equal sign comparison is definitely not equal. The following is the intern pooling of 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 value of wechat3. Since wechat and wechat1 are actually the same, here only take the comparison of wechat and wechat3 as an example to analyze this process.
The state of memory before calling the intern method is shown in the following figure (ignoring S1):
It is not surprising to see whether their values are equal in the picture above. The following is the above code that pooled wechat3 and assigned the pooled result to wechat3. The memory structure changes as follows:
At this point, the two corresponding values are judged again, because the reference and literal values are all the same, so they are equal. We already know the judgment rules of specific intern. If there is a corresponding value in the constant pool, the reference will be returned directly.
There is another situation, that is, what if there is no corresponding value in the constant pool? Take a look at the following code first:
String S2 = "follow"; String wechat4 = new String (S2 + "official account"); wechat4 = wechat4.intern ()
As we said before invoking intern, a String object is created in the heap, and a copy is not stored in the constant pool, as in wechat3's diagram.
The corresponding string does not exist in the constant pool. After calling the intern method, the memory structure is as follows:
After the intern method, a reference to the corresponding string in the heap is stored in the constant pool. In contrast to the above, references can be stored in the JDK7 and then the string constant pool.
It should be noted that when the corresponding string does not exist in the string constant pool, the address returned by calling the intern method is the address in the heap, corresponding to the 0x99 in the figure. The original address of the wechat4 points to the address in the heap, so it will not change.
At this point, if you define another wechat5 with double quotation marks assigned, the code is as follows:
String S2 = "follow"; String wechat4 = new String (S2 + "official account"); wechat4 = wechat4.intern (); String wechat5 = "follow official account"; System.out.println (wechat4 = = wechat5)
When the variable wechat5 is initialized, it is found that a reference already exists in the string constant pool, then wechat5 points directly to this reference, that is, wechat5, like wechat4, points to the String object in memory.
Summary
The key point to note when demonstrating the example above is the reference address returned by the intern method. If the corresponding string already exists in the string constant pool, the address of the string constant is returned [the string is stored in the constant pool]. If the corresponding string does not exist in the string constant pool, the reference in the heap is placed in the position corresponding to the constant pool [the reference of the string in the heap is stored in the constant pool]. In this case, intern returns the reference of the string in the heap.
Figure out the above return logic 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 heap string "hello" and S2 is the address of another "hello" string in the heap. When s1.intern (), the address of S1 is stored in the constant pool, s1.intern () also returns the address of S1, so s1=s3 is all the same address.
Then execute s2.intern (), where there is already a hello string in the constant pool, which is referenced and points to the address of S1. After execution, the address of S1 is returned and assigned to S4, so S1 and S4 also point to the same address, so they are equal.
Through the more in-depth analysis above, you must have a deeper understanding of string constants, string constant pools, and intern methods.
On the JVM string constant pool and String intern method is what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.