The value of String is immutable. Each operation on String will generate a new String object, which is not only inefficient, but also consumes a lot of memory space.
The StringBuffer class is the same as the String class, and is also used to represent strings. However, the internal implementation of StringBuffer is different from String. When string processing is performed, no new objects are generated, and it is better than String in memory usage.
StringBuffer Allocates a 16-byte-length buffer by default. When the string exceeds this size, the buffer length will be automatically increased instead of generating a new object.
StringBuffer is not like String. It can only create objects through new and does not support abbreviation, for example:
StringBuffer str1 = new StringBuffer(); // Allocate a buffer of 16 bytes in length StringBuffer str2 = =new StringBuffer(512); // Allocate a buffer of 512 bytes in length // Storing strings in the buffer , and an empty buffer of 16 bytes of length is reserved behind StringBuffer str3 = new StringBuffer(www.weixueyuan.net);
The main methods of StringBuffer class
The methods in the StringBuffer class mainly focus on the operation of strings, such as appending, inserting and deleting, which is also the main difference between the StringBuffer class and the String class. In actual development, if you need to make frequent modifications to a string, it is recommended to use StringBuffer.
1) append() method
The append() method is used to append content to the end of the current string, similar to a string concatenation. After calling this method, the content of the StringBuffer object also changes, for example:
StringBuffer str = new StringBuffer("biancheng100");str.append(true);
Then the value of object str will become "biancheng100true". Note that the content pointed to by str has changed, not the pointing of str has changed.
The "+" operation of a string is actually to create a StringBuffer object first, then call the append() method to splice the string fragments, and finally call the toString() method to convert it into a string.
In this way, the connection operation of String has more additional operations than StringBuffer, and the efficiency will inevitably be reduced.
However, for smaller strings, the "+" operation is more intuitive and readable, and sometimes you can sacrifice efficiency a little.
2) deleteCharAt()
The deleteCharAt() method is used to delete characters at the specified position and to form a new string of remaining characters. For example:
StringBuffer str = new StringBuffer("abcdef");str. deleteCharAt(3);
This code will delete the character with an index value of 3, i.e. the "d" character.
You can also delete multiple characters at once through the delete() method, for example:
StringBuffer str = new StringBuffer("abcdef");str.delete(1, 4);
This code will delete characters with index values between 1 and 4, including index value 1, but not 4.
3) insert() method
insert() is used to insert a string at a specified location and can be considered an upgraded version of append(). For example:
StringBuffer str = new StringBuffer("abcdef");str.insert(3, "xyz");
The string pointed to by str is abcdxyzef.
4) setCharAt() method
The setCharAt() method is used to modify the characters at the specified position. For example:
StringBuffer str = new StringBuffer("abcdef");str.setCharAt(3, 'z');
This code will modify the character with an index value of 3 to z, and the string pointed to by str is abczef.
The above is just a simple explanation of some commonly used methods. For more methods and explanations, please refer to the API documentation.
Comparison of efficiency between String and StringBuffer
To more clearly see their execution efficiency, the following code adds 26 English letters 10,000 times.
public class Demo { public static void main(String[] args){ String fragment = "abcdefghijklmnopqrstuvwxyz"; int times = 10000; // Pass the String object long tim eStart1 = System.currentTimeMillis(); String str1 = ""; for (int i=0; i<times; i++) { str1 += fragment; } long timeEnd1 = System.currentTimeMillis(); System.out.println("String: " + (timeEnd1 - timeStart1) + "ms"); // By StringBuffer long timeStart2 = System.currentTimeMillis(); StringBuffer str2 = new StringBuffer(); for (int i=0; i<times; i++) { str2.append(fr regime); } long timeEnd2 = System.currentTimeMillis(); System.out.println("StringBuffer: " + (timeEnd2 - timeStart2) + "ms"); }} Running results:
String: 5287msStringBuffer: 3ms
The conclusion is obvious, the execution efficiency of StringBuffer is thousands of times faster than String. This difference becomes more and more obvious as the number of superpositions increases. When the number of superpositions reaches 30,000 times, the running result is:
String: 35923msStringBuffer: 8ms
Therefore, it is highly recommended to use StringBuffer when it involves a lot of string operations.
StringBuilder class
The functions of StringBuilder class and StringBuffer class are basically similar, and the methods are similar. The main difference is that the methods of StringBuffer class are multi-thread-safe, while StringBuilder is not thread-safe. In comparison, the StringBuilder class will be slightly faster.
The CharSequence interface is implemented in StringBuffer, StringBuilder, and String.
CharSequence is an interface that defines string operations. It only includes length(), charAt(int index), subSequence(int start, int end) APIs.
The implementation process of the CharSequence interface of StringBuffer, StringBuilder, and String is different, as shown in the figure below:
It can be seen that String directly implements the CharSequence interface; StringBuilder and StringBuffer are both variable character sequences, both inherit from AbstractStringBuilder and implements the CharSequence interface.
Summarize
Thread Safety:
StringBuffer: thread-safe
StringBuilder: Threads are not safe
speed:
Generally speaking, the speed from fast to slow is StringBuilder > StringBuffer > String, of course this is relative, not absolute.
Usage environment:
Use String to operate a small amount of data;
Use StringBuilder to operate large amounts of data in a single thread;
Multithreading operation of large amounts of data uses StringBuffer.