String pool in Java

Source: Internet
Author: User

JVM uses a string pool to store the characters in Java Program . When a string is used directly for the first time, JVM puts it into the string pool for caching. In general, string objects in the string pool are not recycled. When the program needs to use this string again, you do not need to create a new string to directly point the referenced variable to an existing string in the string. The string object created using the new operation does not point to the object in the string pool, but can point to the object in the string pool using the intern method.

Public class stringdemo {public static void main (string ARGs []) {string str1 = "ABC"; string str2 = "ABC"; string str3 = "def "; string str4 = "abcdef"; string str5 = "ABC" + "def"; // you can find the "abcdef" object in the string pool, therefore, str5 = str4 is truestring str6 = str2 + str3; // It is only known at runtime, that is to say, str1 + str2 is the string str7 = new string ("abcdef") created in the heap "); // The String object created by the new operation does not point to the object string str8 = str7.intern () in the string pool (); // use the intern method to add str7 objects to the string pool if (str1 = str2) {system. out. println ("in this case, the str1 reference variable and the str2 reference variable" + "point to the same memory block in the string pool");} else {system. out. println ("in this case, the str1 reference variable and the str2 reference variable" + "do not point to the same memory block in the string pool");} If (str4 = str5) {system. out. println ("in this case, str4 reference variables and str5 reference variables" + "point to the same memory block in the string pool");} else {system. out. println ("in this case, the str4 reference variable and the str5 reference variable" + "do not point to the same memory block in the string pool");} If (str4 = str6) {system. out. println ("in this case, str4 reference variables and str6 reference variables" + "point to the same memory block in the string pool");} else {system. out. println ("in this case, the str4 reference variable and the str6 reference variable" + "do not point to the same memory block in the string pool");} If (str4 = str7) {system. out. println ("in this case, str4 reference variables and str7 reference variables" + "point to the same memory block in the string pool");} else {system. out. println ("in this case, the str4 reference variable and the str7 reference variable" + "do not point to the same memory block in the string pool");} If (str4 = str8) {system. out. println ("in this case, str4 reference variables and str8 reference variables" + "point to the same memory block in the string pool");} else {system. out. println ("in this case, the str4 reference variable and the str8 reference variable" + "do not point to the same memory block in the string pool ");}}}
Output: in this case, the str1 reference variable and the str2 reference variable point to the same memory block in the string pool. In this case, the str4 reference variable and the str5 reference variable point to the same memory block in the string pool. In this case, str4 references the variable. the reference variable of str6 does not point to the same memory block in the string pool. At this time, the reference variable of str4 and the reference variable of str7 do not point to the same memory block in the string pool. At this time, the reference variable of str4 and the reference variable of str8 is to point to the same memory block in the string pool

Because the string object in the string pool will not be garbage collected, when the string object in a string pool is out of reference, it will become garbage, and the string pool will not be recycled. Therefore, Java Memory leakage occurs. Example:

 
Public class anotherstringdemo {public static void main (string ARGs []) {string str1 = "ABC"; string str3 = "def"; str3 = str1; system. out. println (str3); system. out. println (str1 );}}

Output:

ABC
ABC
(If "def" is not referenced, it will become garbage and Memory leakage will occur)

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.