In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the mystery of the intern method of Java strings, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Learning background
Before entering the intern () method of the text learning string, give these four questions to see if you all know the answers.
1. String S1 = "a" + "b"; / / how many objects have been created?
2. String S2 = new String ("ab"); / / how many objects have been created?
3. String S3 = new String ("a") + new String ("b"); / / how many objects have been created?
4. String S4 = new String ("a") + new String ("a"); s4.intern (); / / how many objects have been created?
If all are clear, congratulations, boss, there is no need to learn, ha!
If you are not sure or need to deepen your understanding, it is recommended to enter the text to understand it!
Of course, you can also pull to the end to have an answer!
String#intern () sample code
Let's first execute a piece of sample code where String calls the intern () method:
Public class StringInternTest {public static void main (String [] args) {String reference1 = new String ("a"); reference1.intern (); String reference2 = "a"; System.out.println (reference1 = = reference2); String reference3 = new String ("a") + new String ("a"); reference3.intern (); String reference4 = "aa"; System.out.println (reference3 = reference4);}}
Output result of JDK1.6 execution:
False
False
Output result of JDK1.7 execution:
False
True
You can think about why the result is like this. I will introduce you in detail next!
String##intern () source code
Let's take a look at the JDK source code of the intern () method as follows:
/ * Returns a canonical representation for the string object. *
* A pool of strings, initially empty, is maintained privately by the * class {@ code String}. *
* When the intern method is invoked, if the pool already contains a * string equal to this {@ code String} object as determined by * the {@ link # equals (Object)} method, then the string from the pool is * returned. Otherwise, this {@ code String} object is added to the * pool and a reference to this {@ code String} object is returned. *
* It follows that for any two strings {@ code s} and {@ code t}, * {@ code s.intern () = = t.intern ()} is {@ code true} * if and only if {@ code s.equals (t)} is {@ code true}. *
* All literal strings and string-valued constant expressions are * interned. String literals are defined in section 3.10.5 of the * The Java ™Language Specification. * @ return a string that has the same contents as this string, but is * guaranteed to be from a pool of unique strings. * / public native String intern ()
Obviously, through the source code, we can see that intern () is a local method of native, but the specific implementation of native source code has been hidden, this is a historical story. During the development of JDK7, due to technological competition and commercial competition, SUN was unable to invest in the research and development of JDK,Oracle and directly acquired Sun. After Oracle took over the research and development of JDK, it published its own Oracle JDK. Many sources, such as Oracle's native underlying code, are hidden, but Oracle officially declares that OpenJDK and Oracle JDK7 and later versions of the source code are almost exactly the same. If you want to understand the specific implementation process of the native underlying source code, you can download the open source OpenJDK source code to view.
OpenJDK official website: https://hg.openjdk.java.net/
GitHub is also open source: https://github.com/openjdk/jdk
For example, the OpenJDK underlying source code main entry corresponding to String: jdk7\ jdk\ src\ share\ native\ java\ lang\ String.c
Java_java_lang_String_intern (JNIEnv * env, jobject this) {return JVM_InternString (env, this);}
The implementation of the underlying method of native, we need to master the syntax of C and C++, the learning threshold is relatively high, here is not the focus of our study, do not do specific introduction.
The function of String#intern () method
The previous English comments on the JDK source code intern () method have already explained the specific use of the intern () method, and there are many explanations on the Internet, but here I briefly summarize the function of the intern () method with my personal understanding and speech skills as follows:
(1) whenever you call intern () of the String object, you will find the string constant pool, and then determine whether the string content of the String object already exists in the constant pool and does not exist, then the object in which the string content is created in the string constant pool (JDK6 and before) or a new reference is created and points to the existing object address in the heap area (after JDK7), and the existence is returned directly.
(2) in the case of JDK7, the string constant pool is separated from the permanent generation and migrated to the heap area. Compared with JDK6, the change is not only the migration of the string constant pool to the heap area, but also the call to the intern () method of the string object. If the object of the string content does not exist in the string constant pool, it will not create the object of the string content directly to the string constant pool like JDK6. Instead, it creates a new reference and points to the address of the existing object in the heap area to achieve the purpose of sharing string constant pool and heap string, which is more efficient.
JDK6 String#intern () execution note
A figure showing the execution of the previous sample code JDK6 is as follows:
/ * JDK6 String#intern () execution instructions * / public class StringInternTest {public static void main (String [] args) {/ / Step6.1 / / created two objects, namely the String object in the heap area and the "a" object in the string constant pool. The reference1 reference points to the object address String reference1 = new String ("a") in the heap area. / / Step6.2 / / determines whether the string constant pool is the string "a", which already exists in the pool, so it returns a reference to the address of the object in the pool reference1.intern () / / Step6.3 / / the string "a" already exists in the string constant pool, so the reference2 reference points directly to the object's address in the string constant pool String reference2 = "a"; / / reference1 points to the object address in the heap area, and reference2 points to the object address in the permanent constant pool, so it is obviously impossible to be the same System.out.println (reference1 = = reference2) / / Step6.4 / / created two objects: the String object in the heap area (the content is "aa") and the "a" object / / reference3 reference in the string constant pool point to the address of the object in the heap area. This process also creates two non-referenced "a" objects in the heap area. String reference3 = new String ("a") + new String ("a") is not discussed here. / / Step6.5 / / determine whether the string constant pool in the permanent generation exists the string "aa", which appears for the first time, so copy the string directly and put it in the pool reference3.intern () The string already exists in the / / Step6.6 / / pool, and the reference2 reference points directly to the address of the object in the persistent string constant pool String reference4 = "aa"; / / similarly, reference3 points to the heap address, and reference4 points to the address in the permanent constant pool, which obviously cannot be the same as System.out.println (reference3 = = reference4);}} JDK7 String#intern () execution description
A figure showing the execution of the previous sample code JDK7 is as follows:
/ * JDK1.7 String#intern () execution instructions * * / public class StringInternTest {public static void main (String [] args) {/ / Step7.1 / / created two objects, namely the String object in the heap area and the "a" object in the string constant pool. The reference1 reference points to the object address String reference1 = new String ("a") in the heap area. / / Step7.2 / / determines whether the string constant pool is the string "a", which already exists in the pool, so it returns a reference to the address of the object in the pool reference1.intern () The string "a" already exists in the string constant pool, so the reference2 reference points directly to the address of the object in the string constant pool String reference2 = "a"; / / reference1 points to the object address in the heap area, reference2 points to the string constant pool in the heap area, and the reference points to a different object address System.out.println (reference1 = = reference2) / / Step7.4 / / two objects are created, namely the String object in the heap area (the content is "aa") and the "a" object in the string constant pool (note that the "aa" object is not created) / / the reference3 reference points to the address of the object in the heap area, which also creates two unreferenced "a" objects in the heap area. String reference3 = new String ("a") + new String ("a") is not discussed here. / / Step7.5 / / determine whether the string "aa" exists in the string constant pool in the heap area. Obviously, this is the first time it appears / / but unlike JDK6, which creates a new object "aa" store, it stores a new reference reference3.intern () that points to the address of the existing object in the heap area. / / Step7.6 / / points to a new reference to the string that already exists in the pool, the reference4 reference points directly to the new reference in the string constant pool, and the new reference points to the existing object address String reference4 = "aa" in the heap area / / reference4 points to the new reference, and the new reference points to the existing object address in the heap, which is the same System.out.println as the reference3 reference (reference3 = = reference4).} how many objects have been created in the classic interview question?
In the actual Java interview, the question of how many objects are created by a string is often asked, mainly to examine the learners' instantiation of objects and how the string constant pool works in the JVM architecture. I think the more common questions are as follows:
1. The simplest example is: String S1 = "a" + "b"; how many objects have been created?
Answer: up to 1, the addition of multiple string constants will be optimized by the compiler to a string constant, namely "ab". If the string constant pool does not exist, the object will be created.
2. Relatively simple ones such as: String S1 = new String ("ab"); how many objects have been created?
Answer: 1 or 2, if you instantiate an object using new, you will inevitably create an object in the heap area, and if the "ab" object does not exist in the string constant pool, the "ab" constant object will be created.
3. Something slightly more difficult, such as String S2 = new String ("a") + new String ("b"); how many objects have been created?
A: at least 4, up to 6
1 new StringBuilder () and 2 new String () in the stack area
The other is the underlying implementation of the toString () method of StringBuilder () is new String (value, 0, count)
The other two, "a" and "b", may create new objects in the constant pool.
Some students may wonder, won't the toString process "ab" string be created in the constant pool?
The answer is, no, in the end, with the call to toString () of StringBuilder, the underlying new String (value, 0, count) does not create a "ab" object in the string constant pool.
The addition of the two new String will be optimized to StringBuilder, and the assembly instructions can be viewed through javac and javap as follows:
Javac InternTest.java
Javap-c InternTest
Public class com.justin.java.lang.InternTest {public com.justin.java.lang.InternTest (); Code: 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. "": () V 4: return public static void main (java.lang.String []) Code: 0: new # 2 / / class java/lang/StringBuilder 3: dup 4: invokespecial # 3 / / Method java/lang/StringBuilder. "": () V 7: new # 4 / / class java/lang/String 10: dup 11: ldc # 5 / / String a 13: invokespecial # 6 / / Method java/lang/String. ": (Ljava/lang/String ) V 16: invokevirtual # 7 / / Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder 19: new # 4 / / class java/lang/String 22: dup 23: ldc # 8 / / String b 25: invokespecial # 6 / / Method java/lang/String. "": (Ljava/lang/String;) V 28: invokevirtual # 7 / / Method java/lang/StringBuilder.append: (Ljava/lang/String ) Ljava/lang/StringBuilder; 31: invokevirtual # 9 / / Method java/lang/StringBuilder.toString: () Ljava/lang/String; 34: astore_1 35: return}
The hardest part is to call the intern () method again, such as:
String S3 = new String ("a") + new String ("b")
How many objects are created by s3.intern ();?
A: at least 4, at most 7
1 new StringBuilder () and 2 new String
The other is the underlying implementation of the toString () method of StringBuilder () is new String (value, 0, count)
In addition, "a" and "b" may create new objects in the constant pool
Finally, when the intern () method is called, it will go to the string constant pool to determine whether "ab" exists or not, JDK6 will create an object "ab", and JDK7 will only create a reference to "ab" and point to the address of the StringBuilder object with the content of "ab" in the heap.
So far, this is the end of this article on the secrets of the intern method of Java strings. For more information about Java intern methods, please search the previous articles or continue to browse the relevant articles below. I hope you will support it in the future!
After reading the above, do you have any further understanding of the secret of the intern method of Java strings? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.