When to use String and StringBuilder in Java

Source: Internet
Author: User

As we all know, the String object is immutable and immutable will cause efficiency problems. When the "+" operator is overloaded for String objects, a New String object is automatically generated.
Some people also say that the StringBuilder will be automatically introduced to solve the efficiency problem in the above problems.

For this reason, I found the answer in Java programming ideas.

The first small example:

package com.linc.TestString;public class TestString {public static void main(String[] args){String mango = "mango";String someting = "abc" + mango + "def" + 47;System.out.println(someting);}}

Run the following command to compile:
Javac TestString. java
Use javap to decompile the above Code to see what happened:
Javap-c TestString

The result is as follows:

Compiled from "TestString.java"public class com.linc.TestString.TestString extends java.lang.Object{public com.linc.TestString.TestString();  Code:   0:aload_0   1:invokespecial#1; //Method java/lang/Object."<init>":()V   4:returnpublic static void main(java.lang.String[]);  Code:   0:ldc#2; //String mango   2:astore_1   3:new#3; //class java/lang/StringBuilder   6:dup   7:invokespecial#4; //Method java/lang/StringBuilder."<init>":()V   10:ldc#5; //String abc   12:invokevirtual#6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   15:aload_1   16:invokevirtual#6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   19:ldc#7; //String def   21:invokevirtual#6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   24:bipush47   26:invokevirtual#8; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;   29:invokevirtual#9; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;   32:astore_2   33:getstatic#10; //Field java/lang/System.out:Ljava/io/PrintStream;   36:aload_2   37:invokevirtual#11; //Method java/io/PrintStream.println:(Ljava/lang/String;)V   40:return}

From the code above, the compiler indeed creates a StringBuilder object.
However, this does not mean that the String object can be used at will. The following is an example:

package com.linc.TestString;public class StringAndBuilder {public String implicit(String[] fields){String result="";for(int i=0;i<fields.length;++i){result+=fields[i];}return result;}public String explicit(String[] fields){StringBuilder result=new StringBuilder();for(int i=0;i<fields.length;++i){result.append(fields[i]);}return result.toString();}}

Decompilation:
[Linc @ localhost TestString] $ javac StringAndBuilder. java
[Linc @ localhost TestString] $ javap-c StringAndBuilder

Compiled from "StringAndBuilder.java"public class com.linc.TestString.StringAndBuilder extends java.lang.Object{public com.linc.TestString.StringAndBuilder();  Code:   0:aload_0   1:invokespecial#1; //Method java/lang/Object."<init>":()V   4:returnpublic java.lang.String implicit(java.lang.String[]);  Code:   0:ldc#2; //String    2:astore_2   3:iconst_0   4:istore_3   5:iload_3   6:aload_1   7:arraylength   8:if_icmpge38   11:new#3; //class java/lang/StringBuilder   14:dup   15:invokespecial#4; //Method java/lang/StringBuilder."<init>":()V   18:aload_2   19:invokevirtual#5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   22:aload_1   23:iload_3   24:aaload   25:invokevirtual#5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   28:invokevirtual#6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;   31:astore_2   32:iinc3, 1   35:goto5   38:aload_2   39:areturnpublic java.lang.String explicit(java.lang.String[]);  Code:   0:new#3; //class java/lang/StringBuilder   3:dup   4:invokespecial#4; //Method java/lang/StringBuilder."<init>":()V   7:astore_2   8:iconst_0   9:istore_3   10:iload_3   11:aload_1   12:arraylength   13:if_icmpge30   16:aload_2   17:aload_1   18:iload_3   19:aaload   20:invokevirtual#5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;   23:pop   24:iinc3, 1   27:goto10   30:aload_2   31:invokevirtual#6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;   34:areturn}

Note that the implicit method forms a loop body from 8th rows to 35th rows. StringBuilder is constructed in a loop body. That is to say, a new StrinBuilder object is created every time a loop is passed.
Let's look at the explicit method again. The code in the loop section is shorter and simpler, and only one StrinBuilder object is generated.

Conclusion:
When writing the toString () method for a class, if the operation is relatively simple, you can trust the compiler, which will reasonably construct the final string result for you. If a loop is used, it is best to create a StringBuilder object by yourself.
If you are not sure which method should be used, use javap to analyze your program!

In addition, linc also translated a small article, is to talk about the difference between String, StringBuffer and StringBuilder: http://blog.csdn.net/lincyang/article/details/6333041

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.