The difference between String, StringBuffer, and StringBuilder

I read a few articles today, and I have a little understanding of String, StringBuffer, and StringBuilder. I recorded it and deepened my impression for later review.

1. First of all, String<StringBuffer<StringBuilder in terms of speed.

  Because in String, he is a string constant, two StringBuffer, StringBuilder is a string variable

  The reason why String is the slowest:

  String is a string constant, while StringBuilder and StringBuffer are both string variables, that is, once a String object is created, the object cannot be changed, but the objects of the latter two are variables and can be changed. Take the following piece of code as an example:

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());

 If you run this code, you will find that "abc" is output first, and then "abcde" is output. It seems that the str object has been changed. In fact, this is just an illusion. The JVM handles these lines of code in this way, first of all Create a String object str, and assign "abc" to str, then in the third line, in fact, the JVM creates a new object also named str, and then adds the value of the original str and "de" Then assign it to a new str, and the original str will be recycled by the JVM's garbage collection mechanism (GC). Therefore, str is not actually changed, that is, the String object mentioned above cannot be changed once it is created. . Therefore, the operation of String objects in Java is actually a process of constantly creating new objects and recycling old objects, so the execution speed is very slow.

  The objects of StringBuilder and StringBuffer are variables, and the operation of variables is to directly change the object without creating and recycling operations, so the speed is much faster than String.

  In addition, sometimes we assign values ​​to strings like this

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());

  In this way, the output results are also "abcde" and "abcde", but the speed of String is much faster than that of StringBuilder, because the operation in line 1 and

  String str="abcde";

  is exactly the same, so it will be fast, and if written in the form

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

  Then the JVM will continue to create and recycle objects to perform this operation as mentioned above. The speed will be very slow.

  2. Let's talk about thread safety

  In terms of thread safety, StringBuilder is thread-unsafe, while StringBuffer is thread-safe

  If a StringBuffer object is used by multiple threads in the string buffer, many methods in StringBuffer can have the synchronized keyword, so thread safety can be guaranteed, but StringBuilder methods do not have this keyword, so thread safety cannot be guaranteed , there may be some wrong operations. So if the operation to be performed is multi-threaded, StringBuffer should be used, but in the case of single thread, it is recommended to use StringBuilder which is faster.

  3. Summarize
  String: suitable for a small number of string operations

  StringBuilder: suitable for a large number of operations on the character buffer under a single thread

  StringBuffer: suitable for a large number of operations in the character buffer under multi-threading

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023153&siteId=291194637