Stringbuilder and stringbuffer are two highly similar classes. stringbuilder is a version rewrite of stringbuffer. The differences between stringbuilder and stringbuffer are as follows: [14] the inheritance relationship between stringbuffer and stringbuilder of basic types in this article, we briefly introduce abstractstringbuilder, which is an abstract model of variable character sequences, it defines the public behavior of variable character sequences. It is an abstract class. For some operations, it provides the default implementation. stringbuilder and stringbuffer are both variable character sequences, so they all implement the abstractstringbuilder method system.
In addition to abstractstringbuilder and charsequence
Actually, they don't have their own methods.
All methods are from abstractstringbuilder and charsequence.As you can see, the method signature of the constructor method-the number and type of the method parameters are the same. In fact, they are the same, and the initial capacity and other thread security
Stringbuffer is thread-safe
Stringbuilder is non-thread-safeActually
Thread security means a synchronized keyword is added before the method.For example, the following methods
Tostringcachestringbuffer has a tostringcache, just like its name,
Cache of the tostring () methodIn short
Cache tostring MethodOf
Each call to tostring will check this field. If it is not null, it will be used to create an object.
If it is null, it will be initialized and assigned a value, that is, the cache.
When any other method is called to change the stringbuffer, The tostringcache is cleared.For example, if the following method setcharat append is called after each change, it is clear that, it is also counterproductive, wasting performance. If tostring is called multiple times, the benefits will be summarized. The functions of the two classes are basically the same logically, they are both variable character sequences. The Code similarity is also very high. They themselves are to do the same thing, but their respective focuses are different. They all implement abstractstringbuilder and charsequence. Their methods both come from these two classes. stringbuffer is thread-safe, in fact, as early as 1.0, stringbuffer already exists in stringbuffer, and stringbuffer was added in 1.5, abstractstringbuilder is also added to stringbuilder in 1.5. It is a non-thread-safe implementation of stringbuffer. abstractstringbuilder is also the final implementation of the class design. The stringbuffer implements this class. Their source code is also mostly the main difference is that the method covered by stringbuffer is slightly more than the following three points. stringbuffer adds the synchronized keyword to the method for synchronization, alternatively, it should be said that stringbuilder has removed the tostringcache cache of synchronizedstringbuffer. If you have removed these three points, the two pieces of code are almost the same unless you are sure you need to use stringbuffer, if string is not variable, if stringbuilder is variable, discard stringbuffer as much as possible.
Stringbuilder is a non-synchronous version of stringbuffer, that is, version rewriting.
Use stringbuilder instead of stringbuffer.
Comparison between stringbuffer and stringbuilder for strings