Java Learning (ii): String, StringBuffer, and StringBuilder

Source: Internet
Author: User

The string class is final, and the string class actually holds strings through a char array. Any change to the string object is to regenerate the new object, and the original object does not move.

1) for the direct addition of strings, the efficiency is very high, because the compiler determines its value, that is, the shape of "I" + "Love" + "Java"; Strings are added, and are optimized for "Ilovejava" during compilation.

For the indirect addition (that is, contains the string reference), the shape is like s1+s2+s3; Efficiency is lower than the direct add, because the compiler does not optimize the reference variable.

2) execution efficiency of String, StringBuilder, StringBuffer:

StringBuilder > StringBuffer > String

Of course this is relative, not necessarily in all cases.

For example, string str = "Hello" + "world" is more efficient than StringBuilder st = new StringBuilder (). Append ("Hello"). Append ("World") is higher.

Therefore, these three classes are each with pros and cons and should be used according to different circumstances:

It is recommended to use string str= "Hello" in the case of a string addition or less modification;

It is recommended to use StringBuilder when the string addition operation is large, and stringbuffer if multi-threading is used.

Interview questions:

1. What is the output of the following code?

String a = "Hello2";   String b = "Hello" + 2; System.out.println ((A = = b));

The output is: true. The reason is simple: "Hello" +2 has been optimized for "Hello2" during compilation, so the variable A and variable B point to the same object during run time.

2. What is the output of the following code?

String a = "Hello2";       String b = "Hello";       String C = b + 2; System.out.println ((A = = c));

The output is: false. Because of the existence of a symbolic reference, String C = B + 2, is not optimized during compilation, does not treat b+2 as a literal constant, so objects generated in this manner are actually stored on the heap. So a and C point to not the same object.

3. What is the output of the following code?

String a = "Hello2";       Final String b = "Hello";       String C = b + 2; System.out.println ((A = = c));

The output is: true. For a final modified variable, a copy is saved in the class file constant pool, that is, not accessed through a connection, and access to the final variable is directly substituted for the actual value during compilation. Then string c = B + 2, which is optimized during compilation: string c = "Hello" + 2;

How many objects are created by 4.String str = new String ("abc")?

And this is where the problem is confusing, and this code actually creates only one object during the run, that is, the "ABC" object is created on the heap. And why are we all talking about 2 objects, here to clarify a concept the code execution process and the class loading process are different. During class loading, it was true that an "ABC" object was created in the run-time pool, and indeed only one string object was created during the execution of the code.

So, if this problem is replaced by string str = new String ("abc"), how many string objects are involved? A reasonable explanation is 2.

5. What is the difference between the following code 1) and 2?

 Public Static void Main (string[] args) {    = "I";     // str1 + = "Love" + "Java";        1)    str1 = str1+ "Love" + "Java";      // 2) }

1) is more efficient than 2, and 1) in the "Love" + "Java" will be optimized during compilation to "Lovejava", and 2) will not be optimized.

6. String str= The difference between "Hello World" and string Str=new string ("Hello World"):

String str = "Hello world"; a literal constant and a symbolic reference are generated during compilation, and the literal constant "Hello World" is stored in the run constant pool during the run. By binding a string object to a reference in this way, the JVM execution engine looks for the same literal constant in the run-time pool, and if it does, points the reference directly to the literal constant that already exists, or creates a space in the run-time pool to store the literal constant. and point the reference to the literal constant. Generating an object from the New keyword is done in the heap, and the process of object generation in the heap does not detect whether the object already exists. As a result, objects are created by using new, even if the content is the same, even if the contents of the string are identical.

(Excerpt from http://www.cnblogs.com/dolphin0520/p/3778589.html)

Java Learning (ii): String, StringBuffer, and StringBuilder

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.