Source String StringBuffer Stringbudlider (2) StringBuffer StringBuilder source Analysis

Source: Internet
Author: User

longitudinal ride cross Fly Zhangyesterday more busy today put StringBuffer Stringbulider of the source analysis presentedbefore we talk about StringBuffer StringBuilder, let's look at their common ancestor.AbstractstringbuilderThis is the foundation of the StringBuilder StringBuffer. And look at what's in the two implementation classes. Obviously, these two things are not written. Let's look at the other door String and see why we saidis the String object immutable StringBuilder stringbuffer variable reason ?It's the top three final. Yes, well, we all know that. The String is immutable, that is, like a disposable paper cup, if you want to 50ml then take 50ml Cup want 100ml to take 100ml cup do not need to change the cup and then is a problem:StringBuffer StringBuilder The bottom is a bunch of arrays. How do they scale ? the so-called capacity is the actual length of the underlying array, such as ArrayList, of course, an arrayThis set of things added is not limited to how to do this effect, with the capacitywe need to know 3 questions about the expansion:1. What is the default capacity? 2. How can I expand my capacity? 3. What is the difference between and length? One more question StringBuffer Why is thread-safe? This is a brief version of the StringBuffer, you can look at it.
 Public Final classStringBufferextendsAbstractstringbuilderImplementsjava.io.Serializable, charsequence{Static Final LongSerialversionuid = 3388685877147921107L;  PublicStringBuffer () {Super(16); }  PublicStringBuffer (intcapacity) {Super(capacity);}  PublicStringBuffer (String str) {Super(Str.length () + 16); Append (str);} /*** This is the same here.*/ Publicstringbuffer (charsequence seq) { This(Seq.length () + 16); append (seq);}  Public synchronized intLength () {returncount;}  Public synchronized intcapacity () {returnvalue.length;}/*** Expansion method provided by JDK directly to external*/ Public synchronized voidEnsurecapacity (intminimumcapacity) {if(Minimumcapacity >value.length) {expandcapacity (minimumcapacity);}}/*** Trim to actual size is array.length---> Count*/ Public synchronized voidtrimtosize () {Super. TrimToSize ();}  Public synchronized voidSetLength (intnewlength) {Super. SetLength (newlength);} Public synchronized CharCharAt (intindex) {if(Index < 0) | | (Index >=count))Throw Newstringindexoutofboundsexception (index);returnValue[index];}  Public synchronized intCodepointat (intindex) {return Super. Codepointat (index);}  Public synchronized intCodepointbefore (intindex) {return Super. Codepointbefore (index);} Public synchronized intCodepointcount (intBeginindex,intEndIndex) {return Super. Codepointcount (Beginindex, EndIndex);} Public synchronized intOffsetbycodepoints (intIndexintCodepointoffset) {return Super. Offsetbycodepoints (index, codepointoffset);}  Public synchronized voidGetChars (intSrcbegin,intSrcend,Chardst[],intDstbegin) {Super. GetChars (Srcbegin, Srcend, DST, dstbegin);}  Public synchronized voidSetcharat (intIndexCharch) {if(Index < 0) | | (Index >=count))Throw Newstringindexoutofboundsexception (index); Value[index]=ch;} /*** Append*/ Public synchronizedstringbuffer append (Object obj) {Super. Append (string.valueof (obj));return  This;}  Public synchronizedStringBuffer Appendcodepoint (intcodepoint) {Super. Appendcodepoint (codepoint);return  This;}  Public synchronizedStringBuffer Delete (intStartintend) {Super. Delete (start, end);return  This;}  Public synchronizedStringBuffer Replace (intStartintend, String str) {Super. Replace (start, end, str);return  This;}  Public synchronizedCharsequence subsequence (intStartintend) {return Super. Substring (start, end);}  Public synchronizedString substring (intStartintend) {return Super. Substring (start, end);}  Public synchronizedStringBuffer Insert (intIndexCharStr[],intOffset,intLen) {Super. Insert (index, str, offset, len);return  This;}  Public synchronizedStringBuffer Insert (intoffset, String str) {Super. Insert (offset, str);return  This;}  Public synchronizedStringBuffer Insert (intOffsetCharstr[]) {Super. Insert (offset, str);return  This;}  Public synchronizedStringBuffer Insert (intDstoffset, charsequence s,intStartintend) {Super. Insert (Dstoffset, S, start, end);return  This;}/*** The default retrieval starting position is 0*/ Public intindexOf (String str) {returnIndexOf (str, 0);}  Public synchronized intIndexOf (String str,intFromIndex) {returnString.IndexOf (value, 0, Count,str.tochararray (),0, Str.length (), fromIndex);}  Public intlastIndexOf (String str) {//Note, synchronization achieved via other invocationsreturnlastIndexOf (str, count);}  Public synchronized intLastIndexOf (String str,intFromIndex) {returnString.LastIndexOf (value, 0, Count,str.tochararray (),0, Str.length (), fromIndex);} /*** Reverse and replace these and order-related methods * In collections there's just a List implementation class (related to order)*/ Public synchronizedStringBuffer Reverse () {Super. reverse ();return  This;}  Public synchronizedString toString () {return NewString (value, 0, count);} Private Static Finaljava.io.objectstreamfield[] Serialpersistentfields ={NewJava.io.ObjectStreamField ("Value",Char[].class),NewJava.io.ObjectStreamField ("Count", Integer.type),NewJava.io.ObjectStreamField ("Shared", Boolean.type),}; Private synchronized voidWriteObject (java.io.ObjectOutputStream s)throwsjava.io.IOException {Java.io.ObjectOutputStream.PutField fields=s.putfields (); Fields.put ("Value", value); Fields.put ("Count", count); Fields.put ("Shared",false); S.writefields ();} Private voidReadObject (java.io.ObjectInputStream s)throwsjava.io.IOException, classnotfoundexception {java.io.ObjectInputStream.GetField fields=s.readfields (); Value= (Char[]) Fields.get ("value",NULL); Count= (int) Fields.get ("Count", 0);}}


StringBuilder is not locked relative to StringBuffer .

Source String StringBuffer Stringbudlider (2) StringBuffer StringBuilder source Analysis

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.