Here we analyze the connection of strings.
1.String
Open the source string,
You will find that the character value that stores the string is the final constant. Looking at the construction method of string, we find that the value value of string is determined in the construction method. It is necessary to explain the following keyword final
The final decorated property is a constant (the value is immutable), or it is assigned at the same time as the declaration, or it is assigned in the constructor method, and once the assignment cannot be changed.
So, string concatenation is implemented with string, because the value of string cannot be changed, so a new string is generated each time the concatenation is stored. Therefore, string concatenation can be used to deal with very low performance.
For more information on string, refer to the blog: http://longpo.iteye.com/blog/2199493
2.StringBuffer
StringBuffer class inherits the abstract class Abstractstringbuilder class, open Abstractstringbuilder source code
Let's take a look at StringBuffer. Three construction methods for overloading
Discovery is the construction method that calls the parent class Abstractstringbuilder
The char array that found StringBuffer's stored data is not final type, stating that it can be changed , and that the constructed string has a space to stitch the string.
In StringBuffer we use the Append () function for concatenation of strings. We can think of that although the char array has remaining, it is certainly not enough to be spliced. So it is necessary to see the append function of the source code implementation.
View the Append method of its parent class Abstractstringbuilder
found that when the value array is insufficient in capacity, a new value array is created to store the string. Here you should understand the principle of stringbuffer string concatenation. When the char value array is insufficient, a larger array is created to store. Efficiency is significantly higher than string.
3.StringBuilder
StringBuilder and StringBuffer are two brothers, the usage is basically the same, the difference is StringBuffer is synchronous, thread-safe, and StringBuilder does not guarantee synchronization, thread insecure.
StringBuilder is faster than StringBuffer in most implementations, and it is recommended to use StringBuilder when a string buffer is accessed by a single thread
Java String choices