There are still people with string concatenation operation when Stringbuilder/stringbuffer replace "+" there is doubt

Source: Internet
Author: User
Tags stringbuffer


First Note:

As for the StringBuilder or StringBuffer to replace the "+", this is the actual application scenarios to consider, see your application needs to take into account thread security or not.

The proposition of every point of view should be justified:

The following are two ways to achieve string concatenation:

public class Testa {public
    static void Main (string[] args) {
        String str = "A";
        StringBuilder sb = new StringBuilder (str);
        Sb.append ("B");
        Sb.append ("C"); 
        System.out.println (str);
    }

Second paragraph:

public class Testb {public
    static void Main (string[] args) {
        String str = "A";
        STR + + "B";
        STR + + "C";
        System.out.println (str);
    }


Then use JAVAP to analyze:

Analysis Results of Testa:

public static void Main (java.lang.string[]);
  Code:
   0:   LDC     #2//string A
   2:   astore_1
   3:   new     #3;//class Java/lang/stringbuilder
   6:   DUP
   7:   aload_1
   8:   invokespecial   #4;//method java/lang/stringbuilder. " <init> ":(ljava/lang/string) V:  astore_2  aload_2
   :  LDC     #5; String B
   :  invokevirtual   #6;//method java/lang/stringbuilder.append: (ljava/lang/string;) ljava/ lang/s
Tringbuilder;
   From:  aload_2:  LDC     #7//string C
   :  invokevirtual   #6; Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s
Tringbuilder;
   :  pop:  getstatic       #8;//field Java/lang/system.out:ljava/io/printstream;
   Aload_1:  invokevirtual   #9//method java/io/printstream.println: (ljava/lang/string;) V
   : Return

}


Analysis Results of TESTB:

public static void Main (java.lang.string[]); code:0: LDC #2; String A 2:astore_1 3:new #3; Class Java/lang/stringbuilder 6:dup 7:invokespecial #4; Method Java/lang/stringbuilder. " <init> ":() V 10:aload_1 11:invokevirtual #5;
   Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s Tringbuilder; 14:LDC #6; String B 16:invokevirtual #5;
   Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s Tringbuilder; 19:invokevirtual #7;
   Method java/lang/stringbuilder.tostring: () ljava/lang/string; 22:astore_1 23:new #3; Class Java/lang/stringbuilder 26:dup 27:invokespecial #4; Method Java/lang/stringbuilder. " <init> ":() V 30:aload_1 31:invokevirtual #5;
   Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s Tringbuilder; 34:LDC #8; String C 36:invokevirtual #5; Method Java/lang/stringbuilder.appEnd: (ljava/lang/string;) ljava/lang/s Tringbuilder; 39:invokevirtual #7;
   Method java/lang/stringbuilder.tostring: () ljava/lang/string; 42:astore_1 43:new #3; Class Java/lang/stringbuilder 46:dup 47:invokespecial #4; Method Java/lang/stringbuilder. " <init> ":() V 50:aload_1 51:invokevirtual #5;
   Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s Tringbuilder; 54:LDC #9; String D 56:invokevirtual #5;
   Method Java/lang/stringbuilder.append: (ljava/lang/string;) ljava/lang/s Tringbuilder; 59:invokevirtual #7;
   Method java/lang/stringbuilder.tostring: () ljava/lang/string; 62:astore_1 63:getstatic #10;
   Field Java/lang/system.out:ljava/io/printstream; 66:aload_1 67:invokevirtual #11;
 Method Java/io/printstream.println: (ljava/lang/string;) V 70:return}

The Testa produced only 1 StringBuilder objects in line 3rd, while Testb produced 3 StringBuilder objects in 3,23,43;

The efficiency of using StringBuilder is significantly higher than using the "+" concatenation string, whether from the complexity of the execution step or the number of StringBuilder instances. Using the "+" concatenation string The JVM converts it to StringBuilder, but each "+" is one more StringBuilder object, but using StringBuilder concatenation string, there is only one StringBuilder object, Also note that when the number of StringBuilder stitching strings is greater than its capacity capacity (default is 16), it expands, so that when the maximum number of stitching can be determined, in the case of the new StringBuilder (), Specifies capacity--StringBuilder sb = new StringBuilder (str,3), which can further reduce space occupancy and improve performance.

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.