Constant Pool string parsing

Source: Internet
Author: User

Online about the string and the article too much, many smattering writers are misleading the public, I am also deeply affected by it, until today read this article (http://www.wtoutiao.com/a/1023451.html), only to brush the layers of fog, found that the implementation of JDK6 and 7 is not the same, post a record.

1 Be sure to note the string s1=new string ("C"); 2 This sentence means that the compilation period in the constant pool generates the string "C". The runtime also generates a String object "C" in the heap, noting that the two strings do not have the slightest relationship. When executing S1.intern (), the compiler goes to the constant pool to find out that the constant pool already exists with the string "C", so nothing is done. This remark will not lead to any result. But if it is
String s2=s1.intern (); S2 points directly to the string in the constant pool. 3 As long as there is a new keyword that is new String ("C"), it will be generated in the heap of "C", the "C" and Chang do not have a relationship.
1 If the string is already in the constant pool, then calling the Intern method will have no consequence, which is equivalent to not being called.  2 only if the constant pool does not contain this string, the call to the Intern method causes the constant pool to generate a relationship with the heap, that is, to create a new "string constant" in the constant pool, that is, the string constant is not a string, but a reference to the string. The real string is inside the heap.
1 important points to emphasize again: 2 S.intern () method action: If there is a string s in the constant pool, the address of the string s in the constant pool is returned directly, if there is no string s in the constant pool, the address of the string s in the heap is placed in the constant pool, and then the address of the string s in the heap is returned. , the constant pool is not actually a string, but a reference to a real string in the heap.
1 look at the following code: (the compiler has generated three strings in the constant pool, namely A, B, AB, the following detailed analysis of the runtime in pairs of newly created string objects)2String s1=NewString ("a") +NewString ("B");3 //Two new Creates a, b two strings in pairs, the plus sign results in a stringbuffer,4The initial content is A,append (b), and then the ToString method is called, resulting in a string object "AB"5String s2=S1.intern ();6 //Check the constant pool because the constant pool does not have the string "ab", so put a reference to the string "AB" in the heap into a constant pool,7 Note that the constant pool is not a real string object, but a reference to the real string object in the heap. 8String s3=NewString ("a") +NewString ("B");//S19String S4=s3.intern ();//The core is here, S4 actually point to is S1, understand here is really understand the JDK7 constant pool. (different from Jdk6)Ten          OneSystem.out.println (s2==s1); ASystem.out.println (s4==S3); -System.out.println (s4==S2);  - Results: the true - false - true  

Finally, it is to be said that the Internet all the way the great God is aware of the Java Virtual machine, Chang very understanding, in fact, are in astray mistakenly, especially this article (http://www.iteye.com/topic/522167), now found in the heap there is nothing so-called " Detain the string area ".

These are some of my understanding, if there are any mistakes, welcome peer criticism.

Constant Pool string parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.