String string constant
StringBuffer String Variable (Thread Safe)
StringBuilder String Variable (non-thread safe)
To put it simply, the main performance difference between String type and StringBuffer type is actually that String is an immutable object. Therefore, every time you change the String type, it is actually equivalent to generating a new String object and pointing the pointer to a new String object. Therefore, it is best not to use String for strings that often change content, because every time the object is generated, it will have an impact on system performance. Especially when there are too many referenced objects in memory, the GC of the JVM will start working, which will definitely be quite slow.
If you use the StringBuffer class, the result will be different. Each time the result will operate on the StringBuffer object itself, instead of generating a new object and then changing the object reference. Therefore, in general, we recommend using StringBuffer, especially when string objects are often changed. In some special cases, the string splicing of String objects is actually interpreted by the JVM as a splicing of StringBuffer objects. Therefore, the speed of String objects is not slower than StringBuffer objects at these times. Especially in the following string objects generation, String efficiency is much faster than StringBuffer:
String S1 = "This is only a" + "simple" + "test";
StringBuffer Sb = new StringBuilder("This is only a").append("simple").append("test");
You will be surprised to find that the speed of generating String S1 objects is simply too fast, and at this time, StringBuffer does not have any advantage in speed. In fact, this is a trick of the JVM. In the eyes of the JVM, this
String S1 = "This is only a" + "simple" + "test"; in fact it is:
String S1 = "This is only a simple test"; so of course it doesn't take too much time. But what you should note here is that if your string comes from another
For String objects, the speed is not that fast, for example:
String S2 = “This is only a”;
String S3 = "simple";
String S4 = "test";
String S1 = S2 + S3 + S4;
At this time, the JVM will do it in a regular manner in the original way
In most cases StringBuffer > String
StringBuffer
Java.lang.StringBuffer thread-safe mutable character sequence. A String-like string buffer, but cannot be modified. Although it contains a certain sequence of characters at any point in time, the length and content of the sequence can be changed by some method calls.
String buffers can be used safely for multiple threads. These methods can be synchronized when necessary, so all operations on any particular instance seem to occur in a serial order that is consistent with the order of method calls made by each thread involved.
The main operations on StringBuffer are the append and insert methods, which can be overloaded to accept any type of data. Each method can effectively convert the given data into a string, and then append or insert 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, if z refers to a string buffer object whose current content is "start", this method call z.append("le") makes the string buffer contain "startle", and z.insert(4, "le") changes the string buffer to contain "starlet".
In most cases StringBuilder > StringBuffer
java.lang.StringBuilde
java.lang.StringBuilder A variable character sequence is new in 5.0. This class provides a StringBuffer-compatible API, but is not guaranteed to be synchronized. This class is designed to be a simple replacement for StringBuffer, when string buffers are used by a single thread (this is common). If possible, it is recommended to take this class first, as it is faster than StringBuffer in most implementations. The methods of both are basically the same.
The above is a compilation of the information on Java String, StringBuffer and StringBuilder. We will continue to add relevant information in the future. Thank you for your support for this website!