Java memory allocation principle (3)

Source: Internet
Author: User

 

Several examples of String constant pool problems

The following is a comparative analysis and understanding of several common examples: String a = "a1"; String B = "a" + 1; System. out. println (a = B); // result = true String a = "atrue"; String B = "a" + "true"; System. out. println (a = B); // result = true String a = "a3.4"; String B = "a" + 3.4; System. out. println (a = B); // result = true analysis: JVM connects the "+" Number of string constants to compile the program, the JVM optimizes the "+" connection of the constant string to the connected value. Take "a" + 1 as an example. After the compiler is optimized, it is already a1 in the class. The value of its String constant is determined during compilation, so the final result of the above program is true. String a = "AB"; String bb = "B"; String B = "a" + bb; System. out. println (a = B); // result = false analysis: JVM references strings because strings are referenced in the "+" connection, however, the referenced value cannot be determined during the compilation period, that is, "a" + bb cannot be optimized by the compiler, the new connection address is allocated dynamically only when the program is running and assigned to B. Therefore, the result of the above program is false. String a = "AB"; final String bb = "B"; String B = "a" + bb; System. out. println (a = B); // result = true analysis: the only difference from [3] Is that the bb string is decorated with final. For final modified variables, it is parsed as a local copy of a constant value during compilation and stored in its own constant pool or embedded in its byte code stream. Therefore, "a" + bb "and" a "+" B "have the same effect. Therefore, the result of the above program is true. String a = "AB"; final String bb = getBB (); String B = "a" + bb; System. out. println (a = B); // result = false private static String getBB () {return "B";} analysis: JVM references String bb, its value cannot be determined during the compilation period. After calling a method during the program running period, the return value of the method is dynamically connected to "a" and the allocated address is B, therefore, the result of the above program is false. The above four examples show that: String s = "a" + "B" + "c"; is equivalent to String s = "abc "; string a = "a"; String B = "B"; String c = "c"; String s = a + B + c; this is different, and the final result is equal: stringBuffer temp = new StringBuffer (); temp. append (). append (B ). append (c); String s = temp. toString (); from the above analysis results, it is not difficult to infer the cause of the low efficiency of the String using the join operator (+), as shown in the following code: public class Test {public static void main (String args []) {String s = null; for (int I = 0; I <100; I ++) {s + = "a" ;}} generates a StringBuilder object every time + is executed, and then drops it after append. When the next loop arrives, A StringBuilder object is generated again, and then the append string is generated. The loop ends until the end. If we directly use the StringBuilder object for append, we can save N-1 time to create and destroy objects. Therefore, for applications that require string connection in a loop, the append operation is generally performed using the StringBuffer or StringBulider object. Understanding and analysis of the intern method of the String object: public class Test4 {private static String a = "AB"; public static void main (String [] args) {String s1 = ""; string s2 = "B"; String s = s1 + s2; System. out. println (s = a); // false System. out. println (s. intern () = a); // true} It is a constant pool problem in Java. For the s1 + s2 operation, a new object is actually created in the heap, and s stores the content of the new object in the heap space, the values of s and a are not equal. When the s. intern () method is called, the address value of s in the constant pool can be returned. Because the value of a is stored in the constant pool, the values of s. intern and a are equal. Summarize the local variable data used in the stack to store some original data types and reference objects (String, array. object, etc.) but does not store the object content heap to store the object created with the new keyword. A string is a special packaging class. Its reference is stored in the stack, and the object content must be determined according to the creation method (constant pool and heap ). some have been created in the compilation phase and stored in the regular string pool, while others are created during runtime. store the new keyword in the heap.

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.