The difference between the String, StringBuilder and StringBuffer three classes

String, StringBuilder and StringBuffer What is the difference between these three classes do their own search for some information from the Internet, after some understanding here tidy, help everyone to watch, but also facilitate the learning process to deepen their own knowledge points memory, where if there is wrong, kindly correct me.

The difference between these three categories are mainly in two aspects, namely speed and security thread in these two areas.

首先说运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder > StringBuffer > String

String slowest reasons:

String a string constant, while StringBuilder and StringBuffer are string variables, namely the String object Once you create the object can not be changed, but the latter two objects are variable, can be changed. The following piece of code as an example:

1 String str="abc";
2 System.out.println(str);
3 str=str+"de";
4 System.out.println(str);

If you run this code will find the first output "abc", then output "abcde", seems to be the object str changed, in fact, this is just an illusion fills, JVM for these lines of code is processed first create a String object str, and the "abc" assigned to str, and then in the third row, in fact, JVM has created a new object also named str, and then the value of the original str and "de" add up re-assigned to new str, str and the original will be JVM's garbage collection (GC) to reclaim lost, so, str not actually been changed, that is, after earlier saying the String object is immutable once created a. Therefore, the operation of String objects in Java is actually a constantly creating new objects and a process to recycle old objects, it performs slowly.

The StringBuilder and StringBuffer object is a variable, the variable operation is to make changes directly to the object, without the creation and recovery operations, so the speed is much faster than the String.

In addition, sometimes we will do this to string assignment

1 String str="abc"+"de";
2 StringBuilder stringBuilder=new StringBuilder().append("abc").append("de");
3 System.out.println(str);
4 System.out.println(stringBuilder.toString());

So that the output result is "abcde" and "abcde", but String StringBuilder speed than the reaction rate is much faster, because this is the operation of the first row and

String str=“abcde”;

Is exactly the same, it will be soon, and if written in this form below

1 String str1="abc";
2 String str2="de";
3 String str=str1+str2;

Then the JVM would like to say above, continue to create, recover the object to this operation. Speed ​​will be very slow.

2. And then it thread-safe

On the security thread, StringBuilder is not thread-safe, while StringBuffer is thread-safe

(1) If a plurality of threads StringBuffer object is used in a string buffer, StringBuffer in many ways with a synchronized keyword, it is possible to ensure that threads are safe;
   (2) but this is not a method StringBuilder keyword , it can not guarantee thread-safe, there may be something wrong operation occurs.
   (3) Therefore, if the operation to be performed is multi-threaded, then we should use StringBuffer, but in the case of single-threaded operation, it is recommended to use the faster StringBuilder.

3. To summarize
  String: Apply a small amount of string manipulation case

StringBuilder: apply to the situation of a large number of operating under a single thread in the character buffer

A large number of cases suitable for multi-threaded operating under the character buffer: StringBuffer

Guess you like

Origin blog.csdn.net/u011732080/article/details/88407968