StringBuilder Append, StringBuffer's append and string str = "a" + "B" difference?

Source: Internet
Author: User

Everyone knows that string+string will spend extra system resources, roughly because string is an immutable class, and each step returns a new string variable, taking up space and time.
In fact, I do not understand this, we look at the bottom of the string+ implementation.

Test case

 Public Static voidMain (string[] args) {String a= "a"; StringBuilder b=NewStringBuilder ("B"); StringBuffer C=NewStringBuffer ("C"); LongStar =System.currenttimemillis ();  for(inti=0;i<200000;i++) {a+ = "a"; }        LongEnd =System.currenttimemillis (); System.out.println ("String:" + (end-star)); Star=System.currenttimemillis ();  for(inti=0;i<200000;i++) {b.append ("B"); } End=System.currenttimemillis (); System.out.println ("StringBuilder:" + (end-star)); Star=System.currenttimemillis ();  for(inti=0;i<200000;i++) {c.append (C); } End=System.currenttimemillis (); System.out.println ("StringBuffer:" + (end-star)); }

Test results

string:17735
Stringbuilder:6
Stringbuffer:7

Test Conclusion
String+ does take up too much resources and is inefficient to handle. StringBuilder newer synchronization methods than StringBuffer, with improved performance.
Cause analysis
I use Javap-verbose to view the compiled class file discovery:

 Public Static voidMain (java.lang.string[]); Flags:acc_public, acc_static code:stack=6, locals=9, args_size=1 0:ldc #16//String a2: Astore_13:New#18//class Java/lang/stringbuilder6: DUP7:LDC #20//String b9:invokespecial #22//Method Java/lang/stringbuilder.<init> ":(ljava/lang/string;) V 12: astore_213:New#25//class Java/lang/stringbuffer16: DUP17:LDC #27//String C19:invokespecial #29//Method java/lang/stringbuffer. "Init> ":(ljava/lang/string;) V 22: Astore_323:invokestatic #30//Method java/lang/system.currentImemillis: () J26:lstore 4 28: Iconst_029:istore 6 31:Goto57 34:New#18//class Java/lang/stringbuilder37: DUP38: Aload_139:invokestatic #36//Method java/lang/string.valueof(Ljava/lang/object;) ljava/lang/String; 42:invokespecial #22//Method Java/lang/stringbuilder.<init> ":(ljava/lang/string;) V 45:LDC #16//String a47:invokevirtual #42//Method Java/lang/stringbuilder.Ppend: (ljava/lang/string;) ljava/lang/StringBuilder; 50:invokevirtual #46//Method Java/lang/stringbuilder.Ostring: () ljava/lang/String; 53: Astore_154:iinc 6, 1 57:iload 6 59:LDC #50//int 10000061:IF_ICMPLT 64:invokestatic #30//Method java/lang/system.currentImemillis: () J67:lstore 6 69:getstatic #51//Field Java/lang/system.out:ljav/io/PrintStream; 72:New#18//class Java/lang/stringbuilder75: DUP76:LDC #55//string string:78:invokespecial #22//Method Java/lang/stringbuilder.<init> ":(ljava/lang/string;) V 81:lload 6 83:lload 4 85: Lsub86:invokevirtual #57//Method Java/lang/stringbuilder.Ppend: (J) ljava/lang/StringBuilder; 89:invokevirtual #46//Method Java/lang/stringbuilder.Ostring: () ljava/lang/String; 92:invokevirtual #60//Method Java/io/printstream.prinLN: (ljava/lang/String;) V95:invokestatic #30//Method java/lang/system.currentImemillis: () J98:lstore 4 100: Iconst_0101:istore 8 103:Goto116 106: Aload_2107:LDC #20//String b109:invokevirtual #42//Method Java/lang/stringbuilder.Ppend: (ljava/lang/string;) ljava/lang/StringBuilder; 112: Pop113:iinc 8, 1 116:iload 8 118:LDC #50//int 100000120:IF_ICMPLT 106 123:invokestatic #30//Method java/lang/system.currentImemillis: () J126:lstore 6 128:getstatic #51//Field Java/lang/system.out:ljav/io/PrintStream; 131:New#18//class Java/lang/stringbuilder134: DUP135:LDC #65//String StringBuilder:137:invokespecial #22//Method Java/lang/stringbuilder.<init> ":(ljava/lang/string;) V 140:lload 6 142:lload 4 144: Lsub145:invokevirtual #57//Method Java/lang/stringbuilder.Ppend: (J) ljava/lang/StringBuilder; 148:invokevirtual #46//Method Java/lang/stringbuilder.Ostring: () ljava/lang/String; 151:invokevirtual #60//Method Java/io/printstream.prinLN: (ljava/lang/String;) V154:invokestatic #30//Method java/lang/system.currentImemillis: () J157:lstore 4 159: Iconst_0160:istore 8 162:Goto175 165: Aload_3166:LDC #27//String C168:invokevirtual #67//Method java/lang/stringbuffer.aPend: (ljava/lang/string;) ljava/lang/StringBuffer; 171: Pop172:iinc 8, 1 175:iload 8 177:LDC #50//int 100000179:IF_ICMPLT 165 182:invokestatic #30//Method java/lang/system.currentImemillis: () J185:lstore 6 187:getstatic #51//Field Java/lang/system.out:ljav/io/PrintStream; 190:New#18//class Java/lang/stringbuilder193: DUP194:LDC #70//String StringBuffer:196:invokespecial #22//Method Java/lang/stringbuilder.<init> ":(ljava/lang/string;) V 199:lload 6 201:lload 4 203: Lsub204:invokevirtual #57//Method Java/lang/stringbuilder.Ppend: (J) ljava/lang/StringBuilder; 207:invokevirtual #46//Method Java/lang/stringbuilder.Ostring: () ljava/lang/String; 210:invokevirtual #60//Method Java/io/printstream.prinLN: (ljava/lang/String;) V213:return

Judging from the compiled code, the exact operation of string+ is:
New StringBuilder ()
New String.valueof ()
Stringbuilder.<init>
Stringbuilder.append ()
Stringbuilder.tostring ()
and the exact operation of StringBuilder is:
Stringbuilder.append ()
The exact operation of StringBuffer () is:
Stringbuffer.append

Transfer from http://alqm1314-126-com.iteye.com/blog/1932879

StringBuilder Append, StringBuffer's append and string str = "a" + "B" difference?

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.