Several examples of java--string constant pool problems

Source: Internet
Author: User
Tags stringbuffer

About string memory allocation Good blog: http://blog.csdn.net/rj042/article/details/6871030


several examples of string constant pool problems

Example 1:

Java code

String s0= "Kvill";

String s1= "Kvill";

String s2= "kv" + "ill";

System.out.println (S0==S1);

System.out.println (S0==S2);

The results are:

True

True

Analysis: First of all, we have to know that the result of the Tao Java will ensure that a string constant has only one copy.

Because the "Kvill" in the S0 and S1 in the example are string constants, they are determined at compile time, so s0==s1 is true, and "kv" and "ill" are string constants, and when a string is concatenated by multiple string constants, it is definitely a string constant, So S2 is also parsed as a string constant at compile time, so S2 is also a reference to "Kvill" in a constant pool. So we come to s0==s1==s2;

Example 2:

Example:

Java code

String s0= "Kvill";

String S1=new string ("Kvill");

String s2= "kv" + New String ("ill");

System.out.println (S0==S1);

System.out.println (S0==S2);

System.out.println (S1==S2);

The results are:

False

False

False

Parsing: A string created with the new string () is not a constant and cannot be determined at compile time, so the string created by new string () is not placed in a constant pool and has its own address space.

S0 or the application of "Kvill" in a constant pool, S1 is a reference to the new object "Kvill" created at run time because it cannot be determined at compile time, S2 is also a newly created object "Kvill" because it cannot be determined at compile time because it has the latter part new String ("ill"). , and you know how to get the results.

Example 3:

Java code

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 for string constant "+" connection, the program compile period, the JVM will be the constant string "+" connection optimization to the value after the connection, take "a" + 1, after the compiler optimization in class is already A1. The value of the string constant at compile time is determined, so the final result of the above program is true.

Example 4:

Java code

String a = "AB";

String BB = "B";

String B = "a" + BB;

System.out.println ((A = = b)); result = False

Analysis: JVM for string reference, because there is a string reference in the string's "+" connection, and the referenced value is not determined at the program compile time, the "a" + BB cannot be optimized by the compiler, and only dynamically assigns and assigns the new address after the connection to B during the program run time. So the result of the above program is false.

Example 5:

Java code

String a = "AB";

Final String bb = "B";

String B = "a" + BB;

System.out.println ((A = = b)); result = True

Analysis: The only difference in [4] is the BB string with the final modification, which, for final-modified variables, is stored in its own constant pool or embedded in its bytecode stream at compile time as a local copy of a constant value. So at this point "a" + BB and "a" + "B" effect is the same. Therefore, the result of the above program is true.

Example 6:

Java code

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 for string reference BB, its value cannot be determined at compile time, the result of the above program is false only after the method is invoked by the program runtime, and the return value of the method and "a" are dynamically connected and assigned the address to B.

about string is immutable

It can be concluded from the example above 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 not the same, the end result is equal to:

Java code

StringBuffer temp = new StringBuffer ();

Temp.append (a). Append (b). append (c);

String s = temp.tostring ();

From the analysis above, it is not difficult to infer that string uses the Join operator (+) inefficiency reason analysis, in the form of such code:

Java code

public class Test {

public static void Main (String args[]) {

String s = null;

for (int i = 0; i < i++) {

S + + "a";

}

}

}

Every time you do it, you create a StringBuilder object and then throw it away after append. Recreate the StringBuilder object when the next loop arrives, and then append the string so that it loops until the end. If we use the StringBuilder object directly to append, we can save N-1 time to create and destroy objects. So for the application of string concatenation in the loop, the StringBuffer or Stringbulider object is generally used for append operation.

Because of the immutable nature of the string class, this one has to say a lot, as long as you know that a string instance once generated will not change again, for example: string str= "kv" + "ill" + "" + "ans"; Is that there are 4 string constants, first "KV" and "ill" generate "Kvill" in memory, and then "Kvill" and "" Generate "Kvill" exist in memory, and then "Kvill ans" is generated, and the address of this string is assigned to STR, It is because string "immutable" produces a lot of temporary variables, which is why it is suggested that StringBuffer be used because StringBuffer is changeable.

final usage and understanding in string

Java code

Final StringBuffer a = new StringBuffer ("111");

Final StringBuffer B = new StringBuffer ("222");

a=b;//This sentence compilation does not pass

Final StringBuffer a = new StringBuffer ("111");

A.append ("222");/compile through

It is visible that final is valid only for reference "value" (that is, memory address), forcing the reference to point only to the object to which it was initially directed, and changing its point will result in a compile-time error. Final is not responsible for the change in the object it points to.

Summary

  A reference (String, Array, object, and so on) of local variable data and objects used in the stack to hold some of the original data types, but no object content

The heap holds objects created using the New keyword.

A string is a special wrapper class, the references are stored on the stack, and the object content must be set differently depending on how it was created (Chang and heap). Some compile-time is already created, stored in a string constant pool, and some runtime is created. Use the new keyword to store in the heap.

Related Article

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.