StringBuffer、StringBuilder、String 区别

StringBuffer, StringBuilder, String distinguish
the difference between these three categories are mainly in two aspects, namely speed and security thread in these two areas.
1. First of all that speed, or the speed of execution is, in this regard speed as the running speed: StringBuilder> StringBuffer> String
reason slowest String:
String a string constant, while StringBuilder and StringBuffer are string variables, namely String Once you create the object after the object can not be changed, but the latter two objects are variable, it can be changed. In the following section of code as an example:
. 1 STR String = "ABC";
2 System.out.println (STR);
. 3 STR + STR = "de";
. 4 System.out.println (STR);
2. for longer thread safe
in the security thread, StringBuilder is not thread-safe, while StringBuffer is thread-safe
synchronized keyword if a StringBuffer object when the string buffer is using multiple threads, with StringBuffer in many ways, it is possible to ensure the threads are safe but StringBuilder approach is not the key, it can not guarantee thread-safe, there may be something wrong operation occurs. So if you want to operate a multi-threaded, then we should use StringBuffer, but in single-threaded case, it is recommended to use the faster StringBuilder.
3. To summarize
  String: Apply a small amount of string manipulation case
  StringBuilder: suitable for a large number of single-threaded operating under the character buffer in case
the situation applicable to a large number of multi-threaded operating under the character buffer: StringBuffer

Guess you like

Origin blog.csdn.net/weixin_45031351/article/details/91038388