StringBuffer is a thread-safe class. To see if this class is thread safe is to see if the method provided by this class is synchronous with the methods of manipulating member variables.
We often compare stringbuffer and string, when we do the concatenation of strings, will choose the StringBuffer append method, the reason is that, that is, append method stitching string
will be more efficient. The reason is that the Append method is to add an operation after the original string, not like a string, changing its value is another space.
By proving that Append's operation is indeed more efficient than "+".
1 Public Static voidMain (String[]args) {2String str= "";3 Longstart0=System.currenttimemillis ();4 for(inti=0;i<10000;i++) {5str+= "Feijishuo";6 }7 Longend0=System.currenttimemillis ();8System.out.println ((end0-start0) + "MS");9 TenStringBuffer sb=NewStringBuffer (); One Longstart1=System.currenttimemillis (); A for(inti=0;i<10000;i++) { -Sb.append ("Feijishuo"); - } the Longend1=System.currenttimemillis (); -System.out.println ((END1-START1) + "MS"); -}
563ms
1ms
The more the strings are spliced, the more obvious the gap is.
The StringBuffer property type is char[] value, which is a modifiable parameter, so you can make a modification to the instance of itself to return it.
Think about it, since the bottom of the StringBuffer is the array type, and the array is fixed-length, to achieve the expansion only the new one array, and then copy the contents of the copy to this
A new array, and then copies the characters that come in append to the back of the new array.
1 Public synchronizedstringbuffer Append (String str) {2 Super. Append (str);3 return This;4 }5 6 Publicabstractstringbuilder Append (String str) {7 if(str = =NULL) str = "NULL";8 intLen =str.length ();9Ensurecapacityinternal (Count +len);TenStr.getchars (0, Len, value, count); OneCount + =Len; A return This; - } - the Private voidEnsurecapacityinternal (intminimumcapacity) { - //overflow-conscious Code - if(Minimumcapacity-value.length > 0) - expandcapacity (minimumcapacity); + } - + voidExpandcapacity (intminimumcapacity) { A intnewcapacity = value.length * 2 + 2; at if(Newcapacity-minimumcapacity < 0) -Newcapacity =minimumcapacity; - if(Newcapacity < 0) { - if(Minimumcapacity < 0)//Overflow - Throw NewOutOfMemoryError (); -Newcapacity =Integer.max_value; in } -Value =arrays.copyof (value, newcapacity); to } + - Public voidGetChars (intSrcbegin,intSrcend,CharDst[],intDstbegin) { the if(Srcbegin < 0) { * Throw Newstringindexoutofboundsexception (srcbegin); $ }Panax Notoginseng if(Srcend >value.length) { - Throw Newstringindexoutofboundsexception (srcend); the } + if(Srcbegin >srcend) { A Throw NewStringindexoutofboundsexception (Srcend-srcbegin); the } +System.arraycopy (value, Srcbegin, DST, Dstbegin, Srcend-srcbegin); -}
Code Analysis:
StringBuffer If you do not need to expand, you will directly put the append in the character, after the array, if you need to expand, will be reborn into a large space of the array. But the same instance is still returned.
Variable class--stringbuffer for Java