How code parses strings

Source: Internet
Author: User

  • STACK: stores basic types (or built-in types) (char, byte, short, Int, long, float, double, Boolean) and object references, data can be shared, which is faster than the register.
  • Heap: used to store objects.
  • String pool: There is a string pool in the Java Virtual Machine (JVM), which stores many string objects and can be shared and used, which improves the efficiency.

    Code parsing:

    Public static void main (string [] ARGs) {/*** scenario 1: The string pool * Java Virtual Machine (JVM) contains a string pool, which stores many string objects; * It can be shared and used, so it improves the efficiency. * Because the string class is final, its value cannot be changed once it is created. * The string pool is maintained by the string class. We can call the intern () method to access the string pool. */String S1 = "ABC"; // The consumer creates an object string S2 = "ABC" in the string pool "; // The Consumer string pool already has the object "ABC" (shared). Therefore, 0 objects are created and a total of system objects are created. out. println ("S1 = S2:" + (S1 = S2); // returns true to point to the same object, system. out. println ("s1.equals (S2):" + (s1.equals (S2); // returns true equals // else ------------------------------------------------------------ over/*** Scenario 2: about New String ("") **/string S3 = new string ("ABC"); // creates two objects, one of which is stored in the string pool, one exists in the heap area; // the producer has another object that references the S3 storage. Put string S4 = new string ("ABC") in the stack; // The "ABC" object already exists in the string pool of the stack. Therefore, only one object system is created in the heap. out. println ("S3 = S4:" + (S3 = S4); // sets false. The address of the S3 stack is different from that of the S4 stack, pointing to different addresses in the stack. system. out. println ("s3.equals (S4):" + (s3.equals (S4); // returns true S3 and S4 have the same value as system. out. println ("S1 = S3:" + (S1 = S3); // The region where false is stored varies. One stack zone and one stack zone are system. out. println ("s1.equals (S3):" + (s1.equals (S3); // returns true with the same value // optional ---------------------------------------------- -------- Over/*** scenario 3: * The constant value is determined (optimized) during compilation. * Here, both "AB" and "cd" are constants, so the value of the variable str3 can be determined during compilation. * This line of code is equivalent to: String str3 = "ABCD"; */string str1 = "AB" + "cd "; // 1 object string str11 = "ABCD"; system. out. println ("str1 = str11:" + (str1 = str11); // ------------------------------------------------------ over/*** scenario 4: * local variable str2, str3 stores the addresses of two detained string objects (intern string objects. ** Line 3 code principle (str2 + str3): * during runtime, JVM will first create a stringbuilder class in the heap, and * use the detention String object pointed to by str2 to complete initialization, * call the append method to merge the detention strings pointed to by str3. * call the tostring () method of stringbuilder to create a String object in the heap, * Finally, store the heap address of the newly generated String object in the local variable str3. ** Str5 stores the address of the detained String object corresponding to "ABCD" in the string pool. * The str4 and str5 addresses are certainly different. ** The memory actually contains five string objects: * three detained string objects, one string object, and one stringbuilder object. */String str2 = "AB"; // 1 object string str3 = "cd"; // 1 object string str4 = str2 + str3; string str5 = "ABCD "; system. out. println ("str4 = str5:" + (str4 = str5); // false // scenario 5: * The Java compiler directly evaluates the string + basic type/constant as a constant expression to optimize it. * When two strings are added at runtime, new objects are generated and stored in heap */string str6 = "B"; string str7 = "A" + str6; string str67 = "AB"; system. out. println ("str7 = str67:" + (str7 = str67); // The variable 'str6' is parsed at runtime. Final string str8 = "B"; string str9 = "A" + str8; string str89 = "AB"; system. out. println ("str9 = str89:" + (str9 = str89); // compile str8 is a constant variable and will be optimized during compilation. // -------------------------------------------------- over}

    Summary:
    1. When using any method to create a String object S, the Java Runtime (running JVM) will hold this X to find whether there is a string object with the same content in the string pool, if it does not exist, a string S is created in the pool. Otherwise, it is not added in the pool.
    2. in Java, as long as the new keyword is used to create an object, a new object will be created (in the heap or stack.
    3. If you create a String object by specifying a string directly or using a String concatenation, you only need to check and maintain the string in the string pool. If no string exists in the pool, you can create one in the pool! However, this string object will never be created in the stack area.
    4. If you use an expression containing variables to create a String object, the system will not only check and maintain the string pool, but also create a String object in the stack area.

  • 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.