Comparison of String StringBuffer and StringBuilder

String String constant
StringBuffer String variable (thread-safe)
StringBuilder String variable (non-thread-safe)

 Briefly, the main performance difference between String type and StringBuffer type is that String is an immutable object, so every time the String type is changed When making changes, it is actually equivalent to generating a new String object, and then pointing the pointer to the new String object, so it is best not to use String for strings that frequently change the content, because every time an object is generated, it will have an impact on system performance. Especially when there are too many unreferenced objects in memory, the GC of the JVM will start to work, and the speed will be quite slow.
 If you use the StringBuffer class, the result is different. Each time the result will operate on the StringBuffer object itself, instead of generating a new object and changing the object reference. So in general we recommend using StringBuffer, especially if the string object changes frequently. In some special cases, the string splicing of String objects is actually interpreted by JVM as the splicing of StringBuffer objects, so the speed of String objects is not slower than StringBuffer objects at these times, especially the following string objects are generated , 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 this time StringBuffer has no advantage in speed at all. In fact, this is a trick of the JVM. In the eyes of the JVM, this
 String S1 = "This is only a" + "simple" + "test"; is actually:
 String S1 = "This is only a simple test"; So of course not It takes too much time. But what everyone should pay attention to here is that if your string is from another String object, the speed is not so 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 the original way



StringBuffer > String StringBuffer
Java.lang.StringBuffer A thread-safe mutable sequence of characters in most cases . A string buffer similar to String, but cannot be modified. Although it contains a certain sequence of characters at any point in time, the length and content of that sequence can be changed by certain method calls.
String buffers are safe to use with multiple threads. These methods can be synchronized when necessary, so that all operations on any particular instance appear to occur in a serial order consistent with the order in which the method calls are 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 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, if z refers to a string buffer object whose current content is "start", this method call z.append("le") will cause the string buffer to contain "startle", and z.insert(4, " le") will change the string buffer to contain "starlet".
StringBuilder > StringBuffer in most cases

java.lang.StringBuilder
java.lang.StringBuilder A variable character sequence is new in 5.0. This class provides an API compatible with StringBuffer, but synchronization is not guaranteed. This class is designed to be used as a drop-in replacement for StringBuffer when the string buffer is used by a single thread (which is common). It is recommended to prefer this class if possible, as it is faster than StringBuffer in most implementations. Both methods are basically the same.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309227&siteId=291194637