What is the difference between string, StringBuilder and StringBuffer?
It is estimated that many java beginners will encounter this problem in the process of learning Java, that is, what is the difference between string, StringBuilder and StringBuffer? I'm here to tidy up today. I hope it will be helpful to you. If there are any problems, please don't hesitate to give advice, so as not to mislead java beginners.
The differences between the three classes are mainly reflected in the following two aspects:
1、 Operating speed (execution speed)
In this aspect, the running speed is: StringBuilder > StringBuffer > string
String slowest reason:
String is a string constant, while StringBuilder and StringBuffer are 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 code as an example:
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, create a string object STR and assign "ABC" to STR, and then in the third line, in fact, the JVM creates a new object also named str, Then add the value of the original STR and "de" and assign it to the new str, The original str will be recycled by the JVM's garbage collection mechanism (GC), so the str has not actually been changed, that is, the string object mentioned above cannot be changed once it is created. Therefore, the operation on the string object 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. The operation on variables is to directly change the object without creating and recycling, so the speed is much faster than that of string.
In addition, sometimes we assign a value to a string as follows:
In this way, the output results are also "ABCDE" and "ABCDE", but the response speed of string is much faster than that of StringBuilder because of the operation and
String str="abcde";
It is exactly the same, so it will be very fast, and if it is written in the following form:
Then the JVM will continue to create and recycle objects for this operation as mentioned above. The speed will be very slow.
2、 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 the StringBuffer can have the synchronized keyword, which can ensure thread safety. However, the StringBuilder method does not have this keyword, so thread safety cannot be guaranteed, and some wrong operations may occur. Therefore, if the operation to be performed is multi-threaded, you need to use StringBuffer. However, in the case of single thread, it is recommended to use a faster StringBuilder.
3、 Summary
reference resources: