The difference between String, StringBuffer and StringBuilder

The difference between these three classes is mainly in two aspects, namely running speed and thread safety.

  1. First of all, let's talk about the running speed, or the execution speed. In this regard, the running speed is: StringBuilder > StringBuffer > String

  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";
2 System.out.println(str);
3 str=str+"de";
4 System.out.println(str);

 

  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

  在线程安全上,StringBuilder是线程不安全的,而StringBuffer是线程安全的

  如果一个StringBuffer对象在字符串缓冲区被多个线程使用时,StringBuffer中很多方法可以带有synchronized关键字,所以可以保证线程是安全的,但StringBuilder的方法则没有该关键字,所以不能保证线程安全,有可能会出现一些错误的操作。所以如果要进行的操作是多线程的,那么就要使用StringBuffer,但是在单线程的情况下,还是建议使用速度比较快的StringBuilder。

  3. 总结一下
  String:适用于少量的字符串操作的情况

  StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况

  StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况

Guess you like

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