[Java development path] (4) Explanation of String, StringBuffer, and StringBuilder
I recently learned about strings and sorted out the String, StringBuffer, and StringBuilder knowledge.
1. String
The String class is located in the java. lang package. After a String object is created, it cannot be modified. It cannot be changed. The so-called modification actually creates a new object and points to a different memory space.
String str1 = "xiaosi";
Str1 = "welcome" + str1;
System. out. println (str1); // welcome to xiaosi.
By observing the running results, we can see that the output of the str1 variable has indeed changed. But why do we always say that the String object is immutable? We are actually deceived by the appearance. JVM parses this code in this way: first create the object str1, assign xiaosi, and then create a new object to execute the second line of code, str1 points to this new object, and the original object has not changed. Because of this mechanism, every time a String is used to operate the String, it is actually constantly creating new objects, the original object will become garbage collected by GC.
Will this reduce the running efficiency? It seems that modifying a code unit is more concise than creating a new string. The answer is: yes, no. The efficiency of creating a new string by splicing "welcome" and "xiaosi" is indeed not high. But immutable characters have a bit: the compiler can share strings.
To find out the specific way of working, you can imagine placing various strings in the Public String storage pool, and the string variable points to the corresponding location in the string storage pool. If you copy a string variable, the original string shares the same character with the copied string variable.
Java designers think that the efficiency brought by sharing is far better than the efficiency brought by String concatenation. Check the program and you will find that you seldom need to modify the string, but often need to compare the string.
2. StringBuffer
Both String and StringBuffer can store and operate strings, that is, String data containing multiple characters. The String class is a String constant and cannot be changed. StringBuffer is a string variable, and its objects can be expanded and modified.
// Create an empty StringBuffer class object.
public StringBuffer()
// Create a StringBuffer class object with the length parameter.
public StringBuffer( int length )
It is a thread-safe variable character sequence. A String buffer similar to a String. The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
The main operations on StringBuffer are append and insert methods. You can reload these methods to accept any type of data. Each method can effectively convert the given data into a string, and then add or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point.
Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically.
3. StringBuilder
From JDK 5, a equivalence class used by a single thread is added for this class, that is, StringBuilder. The StringBuffer and StringBuilder classes have similar functions. The main difference is that the StringBuffer class methods are multithreading and secure, while StringBuilder is NOT thread-safe. Compared with this class, the StringBuilder class should be used first, because it supports all the same operations, but because it does not execute synchronization, It is faster. The StringBuffer and StringBuilder classes should be used for strings that frequently change values.
4. Summary
(1) In general, the speed is from fast to slow: StringBuilder> StringBuffer> String. This comparison is relative, not absolute.
(2) When the string buffer is used by multiple threads, the JVM cannot ensure that the StringBuilder operation is safe. Although the speed is fast, the StringBuffer operation can be correct. In most cases, operations are performed in a single thread. In most cases, StringBuilder is recommended instead of StringBuffer.