Turn: The difference between 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 the 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.StringBuilde
java.lang.StringBuilder一个可变的字符序列是5.0新增的。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。两者的方法基本相同。
转自:https://blog.csdn.net/rmn190/article/details/1492013

Guess you like

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