Source code analysis of Java StringBuilder and StringBuffer
StringBuilder and StringBuffer are two commonly used classes for operating strings. As we all know, StringBuilder is thread unsafe, while StringBuffer is thread safe. The former is jdk1 5 added, the latter in jdk1 0. Let's analyze their internal implementation.
1、 Inheritance relationship
As like as two peas, we can see that the inheritance relationship between the two classes is the same. Serializable is a flag that can be serialized. The charsequence interface includes the methods charat (), length (), subsequence (), toString (), and the string class also implements this interface. The focus here is on the abstract class abstractstringbuilder, which encapsulates the implementation of most operations of StringBuilder and StringBuffer.
2、 Abstractstringbuilder
1. Variable and construction method
Abstractstringbuilder internally uses a char [] array to save strings, and the initial capacity method can be specified during construction.
2. Capacity expansion
The capacity expansion method is finally implemented by expandcapacity(). In this method, first expand the capacity to the original capacity plus 2. If it is still less than the specified capacity, set the new capacity to minimumcapacity. Then judge whether it overflows. If it overflows, set the capacity to integer MAX_ VALUE。 Finally, copy the value, which is obviously a time-consuming operation.
3. Append() method
Append () is the most commonly used method, which has many forms of overloading. The above is one of them, which is used to append strings. If STR is null, the appendnull () method is called. This method actually appends the characters' n ',' U ',' l 'and' l '. If it is not null, first expand the capacity, then call the getChars () method of String to append STR to the end of value. Finally, the object itself is returned, so append () can be called continuously.
3、 StringBuilder
Abstractstringbuilder has implemented most of the required methods. StringBuilder and StringBuffer only need to be called. Let's take a look at the implementation of StringBuilder.
1. Constructor
As you can see, the default capacity size of StringBuilder is 16. Of course, you can also specify the initial capacity or assign an initial value to the StringBuilder object with an existing character sequence.
2. Append() method
There are many overloaded methods of append (). Here are just two. Obviously, this is the method in the parent class abstractstringbuilder called directly.
3、toString()
The toString () method returns a new string object that does not share memory with the original object. In fact, the same is true for the substring () method in abstractstringbuilder.
4、 Sringbuffer
Stiringbuffer is similar to StringBuilder, but in order to achieve synchronization, many methods use lsynchronized modification, such as the following methods:
You can see that synchronized is added in front of the method. In addition, there is a variable tostringcache in the append () and setlength () methods above. This variable is used to cache the last toString () method. Whenever the StringBuffer is modified, this variable will be assigned null. The toString of StringBuffer is as follows:
In this method, if tostringcache is null, it is cached first. The final returned string object is a little different. This constructor also has a parameter true. Find the source code of string and take a look:
Originally, the string object constructed by this construction method does not actually copy the string, but points the value to the construction parameter, which is to save the time of copying elements. However, this constructor has package access rights and cannot be called under normal circumstances.