In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the "brief introduction of Java string constant pool and literal assignment". In daily operation, I believe many people have doubts about the simple introduction of Java string constant pool and literal assignment. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java string constant pool and literal assignment". Next, please follow the editor to study!
String constant pool
The reason for the existence of the string constant pool is that String string types are more complex than the eight basic types, and they are used more frequently, and the frequent creation of string objects will cause performance bottlenecks, so the similarity mechanism is used to reuse strings (shared meta mode).
After JDK 1.7 (inclusive), the string constant pool has been moved from the method area to the heap.
Literal assignment String S1 = "ancient kites"
The above is the most common way for string variables, which is called literal declaration, by enclosing the string in double quotes and assigning a value to a variable. In this case, the string is put directly into the string constant pool and then returned to the variable.
So if I declare another string with the same content, I will find that it already exists in the string constant pool, so just point directly to the address in the constant pool.
For example, as shown in the figure above, S1 and S2 are declared and end up pointing to the address of the same constant pool, so the result of S1 address = S2 is true.
New String () mode
Use the method of new String (), but it is basically not recommended unless there is a special logical need.
String a = "ancient"; String S2 = new String (a + "kite")
When you declare a string variable in this way, two things happen
The same string already exists before the string constant pool.
For example, before using new, a variable was declared literally, and a string constant with the same content already existed in the string constant pool.
First, an object reference to the S2 variable is created in the heap
Then point the object reference to an existing constant in the string constant pool
Constants with the same content do not exist in the string constant pool
This string has not been used anywhere before, and the first time the string is declared is new String (), in which case a string object is created directly in the heap and returned to the variable.
I have seen a lot of places saying that if the string constant pool does not exist, put the string in first, and then reference the constant object of the string constant pool. This statement is problematic, but new String (), if there is no one in the pool, it will not put a copy into it.
Based on this characteristic of new String (), we can draw a conclusion:
String S1 = "ancient kite"; String a = "ancient"; String S2 = new String (a + "kite"); String S3 = new String (a + "kite"); System.out.println (s1==s2); / / falseSystem.out.println (s2==s3); / / false
The output of the above code must be false, because new String () whether you have it in the constant pool or not, I will create a new object in the heap, and the newly created object will certainly not be equal to other objects.
Intern () pooling
When will it be put into the string constant pool, after using the intern () method. The definition of intern ():
If the content of the current string exists in the string constant pool, the condition is that the equas () method is true, that is, the content is the same, then directly return the reference of the string in the constant pool
If you are not previously 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 address in the constant pool.
In the first case, the string to be pooled is the same as the string in the string constant pool (equas () judgment)
String S1 = "ancient kite"; String a = "ancient"; String S2 = new String (a + "kite"); S2 = s2.intern ()
This string constant already exists in the constant pool, so a new object S2 is new and an object with the same string content is created in the heap.
At this point, S1 = S2 returns fasle. Then we call S2 = s2.intern (), assign the result returned by the pooling operation to S2, and the following change occurs.
At this point, judging S1 = S2 again returns true because they all point to the same string in the string constant pool.
In the second case, there is no string with the same content in the string constant pool
Use new String () to create a string object in the heap
What happens after using intern ()? a new object is added to the constant pool, but instead of copying the string to the constant pool, it points directly to a string object that already exists in the heap.
Because after JDK 1.7, the string constant pool may not necessarily store the string object, but may also store a reference to the address in the heap. This is the case now. Note that the following figure only calls s2.intern () and does not return to a variable. Where the string constant pool (0x88) points to the string object (0x99) in the heap is the process of intern ().
Only when we return the result of s2.intern () to S2 does S2 really point to the string constant pool.
Public static void main (String [] args) {String S1 = "ancient kite"; String S2 = "ancient kite"; String a = "ancient kite"; String S3 = new String (a + "kite"); String S4 = new String (a + "kite"); System.out.println (S1 = = S2) / / [1] true System.out.println (S2 = = S3); / / [2] false System.out.println (S3 = = S4); / / [3] false s3.intern (); System.out.println (S2 = = S3); / / [4] false S3 = s3.intern (); System.out.println (S2 = = S3) / / [5] true S4 = s4.intern (); System.out.println (S3 = = S4); / / [6] true}
When ① uses string literals, it creates objects in the constant pool, which, of course, must be after the constant is collapsed.
When ② uses new String (), the string object produced by new is in the heap, not in the constant pool.
Intern () has changed since ③ JDK7, and now if this object does not exist in the constant pool, it will not be copied to the constant pool, but simply use a string object already in the heap.
④ JDK7's previous intern () is not like this. It used to create a new object in the constant pool. You can test your code in JDK6, and the results should be different. So, your problem is not on new String (), but on intern (), which has nothing to do with constant pooling.
At this point, the study of "a brief introduction to Java string constant pool and literal assignment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.