String
String: String constant, the string length is immutable. In Java, String is immutable.
The String class contains the following definition:
/** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count;
The array used to store characters is declared as final, so it can only be assigned once and cannot be changed again.
StringBuffer(JDK1.0)
StringBuffer: String variable (Synchronized, i.e., thread-safe). If you need to frequently modify string content, for efficiency reasons it is best to use StringBuffer. If you want to convert it to String type, you can call StringBuffer's toString() method.
Java.lang.StringBufferA thread-safe mutable character sequence. At any point in time it contains some specific character sequence, but the length and content of the sequence can be changed through certain method calls. String buffers can be safely used by multiple threads.
StringBufferThe main operations on it are the append and insert methods, which can be overloaded to accept any type of data. Each method effectively converts the given data into a string, and then appends or inserts the characters of that string into the string buffer.
- The append method always adds these characters to the end of the buffer;
- the insert method adds characters at the specified point.
For example, ifzrefers to a string buffer object whose current content isstart, then this method callz.append("le")will make the string buffer containstartle, andz.insert(4, "le")will change the string buffer so that it containsstarlet 。
StringBuilder(JDK5.0)
StringBuilder: String variable (not thread-safe). Internally, a StringBuilder object is treated as a variable-length array containing a character sequence.
java.lang.StringBuilder is a mutable character sequence, newly added in JDK 5.0. This class provides an API compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for StringBuffer, used when the string buffer is used by a single thread (this situation is very common).
Its constructors are as follows:
| Constructor | Description |
| StringBuilder() | Creates a StringBuilder object with a capacity of 16 (16 empty elements) |
| StringBuilder(CharSequence cs) | Creates a StringBuilder object containing cs, with 16 empty elements appended at the end |
| StringBuilder(int initCapacity) | Creates a StringBuilder object with a capacity of initCapacity |
| StringBuilder(String s) | Creates a StringBuilder object containing s, with 16 empty elements appended at the end |
In most cases,StringBuilder > StringBuffer. This is mainly because the former does not need to consider thread safety.
Differences between the three
The main performance difference between String type and StringBuffer: String is an immutable object, so every time a String type is changed, a new String object is generated, and then the pointer is pointed to the new String object. Therefore, strings whose content changes frequently should not use String, because generating an object each time will affect system performance, especially when there are many unreferenced objects in memory, the JVM's GC will start working, and performance will decrease.
When using the StringBuffer class, operations are performed on the StringBuffer object itself each time, rather than generating new objects and changing object references. Therefore, in most cases, StringBuffer is recommended, especially when string objects change frequently.
In certain special cases, string concatenation of String objects is actually compiled by the Java Compiler into StringBuffer object concatenation, so at these times String objects are not slower than StringBuffer objects, for example:
String s1 = “This is only a” + “ simple” + “ test”; StringBuffer Sb = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);
The speed of generating the String s1 object is not slower than StringBuffer. In fact, in the Java Compiler, the following conversion is automatically performed:
The Java Compiler directly compiles the first statement above into:
String s1 = “This is only a simple test”;
So the speed is very fast. But it should be noted that if the concatenated strings come from other String objects, the Java Compiler will not automatically convert them, and the speed will not be as fast, for example:
String s2 = “This is only a”; String s3 = “ simple”; String s4 = “ test”; String s1 = s2 + s3 + s4;
At this time, the Java Compiler will do it strictly in the original way. The concatenation operation of String (i.e., +) is implemented using the append method of StringBuilder (or StringBuffer). At this time, for the above situation, if s2, s3, and s4 are defined as String, an additional StringBuffer (or StringBuilder) needs to be created during concatenation, and then the StringBuffer is converted to String. If StringBuffer (or StringBuilder) is used, there is no need to create an additional StringBuffer.
Usage strategies
-
(1) Basic principle: If you want to operate on a small amount of data, use String; for single-threaded operation on large amounts of data, use StringBuilder; for multi-threaded operation on large amounts of data, use StringBuffer.
-
(2) Do not use the "+" of the String class for frequent concatenation, because the performance is extremely poor. You should use the StringBuffer or StringBuilder class. This is a relatively important principle in Java optimization. For example:
String result = ""; for (String s : hugeArray) { result = result + s; } // 使用StringBuilder StringBuilder sb = new StringBuilder(); for (String s : hugeArray) { sb.append(s); } String result = sb.toString();When the above situation occurs, obviously we should use the second method, because the first method, each loop willcreate a String result to save the result. Apart from this, the two are basically the same (forJDK 1.5 and later versions)。
-
(3) In order to obtain better performance, you should specify their capacity as much as possible when constructing StringBuffer or StringBuilder. Of course, if the length of the string you operate on does not exceed 16 characters, you don't need to specify it. When capacity is not specified, an object with a capacity of 16 is constructed by default. Not specifying the capacity will significantly reduce performance.
-
(4) StringBuilder is generally used inside methods to complete similar+functions. Because it is not thread-safe, it can be discarded after use. StringBuffer is mainly used in global variables.
-
(5) Under the same conditions, using StringBuilder compared to using StringBuffer can only achieve a performance improvement of about 10%~15%, but it risks the danger of not being thread-safe. In real modular programming, the programmer responsible for a certain module may not be able to clearly determine whether the module will run in a multi-threaded environment. Therefore: unless you are sure that the system bottleneck is on StringBuffer, and you are sure that your module will not run in multi-threaded mode, you can use StringBuilder; otherwise, still use StringBuffer.
Article source: https://blog.csdn.net/kingzone_2008/article/details/9220691