These two classes are inherited from Abstractstringbuilder,abstractstringbuilder with two member properties
1 Char [] value; 2 int count;
The former is used to store strings, which are used to count how many bits are actually used, that is, the actual length of the string. When new is a StringBuilder or StringBuffer, the default capacity is 16, which is the declaration of a 16-size character array, at which time count=0,value.length=16.
1 Public StringBuilder () // initial char[] length2 public stringbuffer () // initial char[] length is 3 4 Public StringBuilder (String str) // initial char[] length is str.length+165 Public StringBuffer (String str) // initial char[] length is str.length+16
In StringBuilder, there is essentially no redefinition of what methods are defined in the parent class abstractstringbuilder of the super call.
Similar in StringBuffer, there is basically no redefinition of what method, also is called the parent class Abstractstringbuilder in the method, but the time to call the method is added synchronzied, increase the synchronization lock to ensure thread safety. The object instance that is called by the method is preceded by the synchronized Action object.
In addition, a private member variable is added to the StringBuffer:
1 Private transient Char [] Tostringcache
Transient short-term meaning, is a keyword, with its declared instance variable, its value does not need to maintain, in other words, serialization will not participate in the serialization process.
//StringBuilder PublicString toString () {//Create a copy, don ' t share the array return NewString (value, 0, Count); } //StringBuffer Public synchronizedString toString () {if(Tostringcache = =NULL) {Tostringcache= Arrays.copyofrange (value, 0, Count); } return NewString (Tostringcache,true); }
In the ToString method, StringBuffer copies the contents to Tostringcache, and then constructs a string. Tostringcache will be empty when any modification is null.
The following is a direct look at the method of the Abstractstringbuilder class.
1 Public void ensurecapacity (int minimumcapacity)
This method is used to enlarge the capacity of value, minimumcapacity if it is greater than value.length newcapacity = max{value.length*2 + 2, minimumcapacity}, if newcapcity > max_array_size the final capacity = Max{minimumcapacity, max_array_size}. where max_array_size = integer.max_value-8.
Various append methods are defined in the class, and if the parameters of the Append method are null, the append result is that the "null" string is appended to the existing character array, not nothing. If Append (Boolean), "true" or "false" is added to the end of an existing character array.
Abstractstringbuilder is primarily defined by various append and insert methods. The difficulty is not big, here does not repeat ~ ~
Java.lang.StringBuilder and Java.lang.StringBuffer