Before reading this essay, be sure to look at the previous two chapters:
Java string, StringBuilder, StringBuffer common source analysis and comparison (a): String source code Analysis
Java string, StringBuilder, StringBuffer common source analysis and comparison (ii): StringBuilder, StringBuffer source analysis
This explains the three types of source code, but also more or less explain their differences.
Below to get to the chase:
Same point:
First, member variables:
1. Through the previous analysis of the source code, you can know that they are all through the char array value to save the string
Both 2.StringBuilder and StringBuffer inherit from the Abstractstringbuilder, and their construction methods are basically derived from Abstractstringbuilder
3.String, StringBuilder, StringBuffer all implement the Charsequence interface, this interface defines the following methods
/*** Returns The length of this character sequence. The length is the number * of 16-bit <code>char</code>s in the sequence.</p> * *@returnThe number of <code>char</code>s in this sequence*/ intlength (); /*** Returns The <code>char</code> value at the specified index. An index ranges from zero * to <tt>length ()-1</tt>. The first <code>char</code> value of the sequence is at * Index Zero, the next at index one, and so on, as For array * indexing. </p> * <p>if The <code>char</code> value specified by the index is a * <a href= "{ @Docroot}/java/lang/character.html#unicode ">SURROGATE</A>, the surrogate * value is returned. * * @paramindex The index of the <code>char</code> value to being returned * *@returnThe specified <code>char</code> value * *@throwsindexoutofboundsexception * If the <tt>index</tt> argument is negative or not less than * <tt>length () </tt>*/ CharCharAt (intindex); /*** Returns A new <code>CharSequence</code> that's a subsequence of this sequence. * The subsequence starts with the <code>char</code> value @ the specified index and * ends with the <c Ode>char</code> value at index <tt>end-1</tt>. The length * (in <code>char</code>s) of the * returned sequence is <tt>end-start</tt>, s o if <tt>start = = end</tt> * Then a empty sequence is returned. </p> * *@paramstart the start index, inclusive *@paramEnd the end index, exclusive * *@returnThe specified subsequence * *@throwsindexoutofboundsexception * If <tt>start</tt> or <tt>end</tt> is negative, * If <tt>end</tt> is greater than <tt>length () </tt>, * or if <tt>star T</tt> is greater than <tt>end</tt>*/charsequence subsequence (intStartintend); /*** Returns A string containing the characters in this sequence with the same * order as this sequence. The length of the string would be the length of * this sequence. </p> * *@returnA string consisting of exactly this sequence of characters*/ PublicString toString ();
Ii. Method of Membership:
Most of the methods in 1.StringBuilder and StringBuffer come from their common parent class abstractstringbuilder, which is basically exactly the same.
Different points:
First, member variables
1. Although string, StringBuilder, and stringbuffer all store strings as char array value, the value in string is final (that is, the constructor cannot be modified after initialization), while the remaining two are not, So when splicing, string needs to keep the new string object, and the copy method to achieve stitching, and StringBuilder, StringBuffer can be in the value of sufficient capacity in the case of value after the concatenation of the string;
Ii. Method of Membership
1. In string concatenation, there are two methods, ' + ' is the same as the Concat method, the implementation of the same effect, the difference is that the concat is not the argument of the null, if the parameter is NULL, will be reported null pointer exception, The remaining two are only append method stitching and the stitching will not create new objects (Stringbuilder,stringbuffer);
2.StringBuilder, StringBuffer a thread-safe, a thread is not secure, in fact, the reason is from the StringBuffer append method uses the synchronized to achieve not allow multi-threaded simultaneous access;
Doubt Point:
In StringBuilder, StringBuffer in fact there is a different point, first give Abstractstringbuilder, StringBuffer, StringBuilder about this part of the source
Abstractstringbuilder:
// documentation in subclasses because of synchro difference Public abstractstringbuilder Append (stringbuffer sb) { ifnull) return append ("null"); int len = sb.length (); + len); Sb.getchars (0, Len, value, count); + = len ; return This ; }
StringBuffer:
/** * Appends the specified <tt>StringBuffer</tt> to this sequence. * <p> * The characters of the <tt>StringBuffer</tt> argument is appended, * in order, to the CO Ntents of this <tt>stringbuffer</tt>, increasing the * length of this <tt>StringBuffer</tt> by The length of the argument. * If <tt>sb</tt> is <tt>null</tt> and then the four characters * <tt> "null" </tt> was appended to this <tt>stringbuffer</tt>. * <p> * Let <i>n</i> being the length of the old character sequence, the one * contained in the < ;tt>stringbuffer</tt> just prior to execution of the * <tt>append</tt> method. Then the character at index <i>k</i> in * The new character sequence are equal to the character at index &L T;i>k</i> * in the old character sequence, if <i>k</i> was less than <i>n</i>; * Otherwise, it's equal to the character at index <i>k-n</i> in the * argument <code>sb</code> ;. * <p> * This method synchronizes in <code>this</code> (the destination) * object but does not sy Nchronize on the source (<code>sb</code>). * *@paramSB the <tt>StringBuffer</tt> to append. * @returna reference to this object. * @since1.4*/ Public synchronizedstringbuffer Append (stringbuffer sb) {Super. Append (SB); return This; }
StringBuilder:
//appends the specified string builder to this sequence. PrivateStringBuilder Append (StringBuilder sb) {if(SB = =NULL) returnAppend ("null"); intLen =sb.length (); intNewcount = Count +Len; if(Newcount >value.length) expandcapacity (Newcount); Sb.getchars (0, Len, value, count); Count=Newcount; return This; } /** * Appends the specified <tt>StringBuffer</tt> to this sequence. * <p> * The characters of the <tt>StringBuffer</tt> argument is appended, * in order, to this s Equence, increasing the length of this sequence by the length of the argument. * If <tt>sb</tt> is <tt>null</tt> and then the four characters * <tt> "null" </tt> was appended to this sequence. * <p> * Let <i>n</i> being the length of this character sequence just prior to * execution of the & Lt;tt>append</tt> method. Then the character @ index * <i>k</i> in the new character sequence are equal to the character at * Dex <i>k</i> in the old character sequence, if <i>k</i> was less than * <i>n</i>; Otherwise, it is equal to the character at index <i>k-n</i> * in the argument <code>sb</code>. * *@paramSB the <tt>StringBuffer</tt> to append. * @returna reference to this object. */ PublicStringBuilder Append (stringbuffer sb) {Super. Append (SB); return This; }
You can see that in the parent class, StringBuffer have append (StringBuffer sb) This method, but there is no append (StringBuilder sb), only StringBuilder have, but also defined as private, I think maybe this is also related to StringBuilder, StringBuffer thread safety, but do not think of specific details, leaving a doubt point, want to know the small partner can give the answer, thank you!
Java string, StringBuilder, StringBuffer common source analysis and comparison (three): string, StringBuilder, stringbuffer comparison