Variable class--stringbuffer for Java

Source: Internet
Author: User

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

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.