On StringBuffer and StringBuilder

StringBuffer和StringBuilder

Strings are our most popular classes, we are using every day, we all know that a string String class is immutable
, we sometimes need to use string concatenation in the preparation of the code, creates a new mosaic at the time string object, the efficiency is not that slow but also a waste of memory, severe cases may appear OutOfMemoryError, memory overflow.
We recommend using java StringBuffer or StringBuilder in a large number of string concatenation when these two classes, they can be reused string string concatenation, they inherit their internal AbstractStringBuilder class so large in some places to achieve very similar.
First we look at their constructors

StringBuffer

Here Insert Picture Description
Here Insert Picture Description

StringBuilder

Here Insert Picture Description
Here Insert Picture Description

Can be seen from the above default capacity is 16, in the case where the transfer is worth the string length based on the original +16

We look at this method Arrays.copyOf (boolean [] original, int newLength), this method can copy the array to a new array to achieve the expansion of the array, System.arraycopy (Object src, int srcPos, Object dest, int how destPos, int length) this method can copy the contents of the specified array to another array, this method can achieve volume reduction of the array, we take a look append StringBuilder class () method implementation
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

We can see that in the first string concatenation when judgment is passed in is not null if a null string will be spliced out, if not null, then will the original length of the array is passed in your original string length were added together, that is, count + len, count records was passed in string length, when he exceeds the original length expansion will take place 16:00, the expansion mechanism is 1 + 2 times, expansion when using Arrays.copyOf () this method Since there will be a lot of empty location for the expansion of this method, it is also called System.arraycopy () this method
by the above methods can be seen in the expansion mechanism they use to reuse an array of arrays, but also has some problems.

StringBuilder thread safety issues

When multiple threads access StringBuilder is very easy to thread-safety issues arise, how to do it?
This time it came up StringBuffer we look at its source code Here Insert Picture Description
synchronized method can guarantee that almost all thread-safe StringBuffer class are added greatly to ensure the synchronized thread-safe, but there are problems, not the speed of StringBuffer StringBuilder fast

The difference between StringBuilder and StringBuffer

StringBuilder
  1. Thread safe
  2. Version 1.5
  3. high speed
StringBuffer
  1. Thread Safety
  2. Version 1.0
  3. Slow

These are my understanding of StringBuffer and StringBuilder welcome the guidance of the great God

Guess you like

Origin blog.csdn.net/weixin_45118251/article/details/90550007