String literal constant
StringBuffer string variable (thread safe)
StringBuilder string variable (non-thread safe)
Briefly, the main performance difference between the string type and the StringBuffer type is actually that the string is an immutable object, so each time a change is made to the string type it is actually equivalent to generating a new string object and then pointing the pointer to the new STRI Ng object, so it is best not to use string to change the content of the strings, because each generation of objects will have an impact on system performance, especially when there is no reference object in memory, the JVM's GC will start to work, the speed will be quite slow.
Assuming that the StringBuffer class is used, the result is different, and each time the result is manipulated against the StringBuffer object itself, instead of generating a new object and changing the object reference. So under normal circumstances we recommend using StringBuffer, especially if the string object is often changed. In some special cases, string concatenation is actually interpreted by the JVM as a concatenation of StringBuffer objects. So at these times, the speed of the String object is not slower than the StringBuffer object. In particular, in the following String object generation, string efficiency is far faster than StringBuffer:
String S1 = "This was only a" + "simple" + "test";
StringBuffer Sb = new StringBuilder ("This was only a"). Append ("simple"). Append ("test");
You will be surprised to find that the speed of generating a String S1 object is simply too fast. And at this time StringBuffer unexpectedly speed at all not at all dominant.
In fact, this is a trick of the JVM. In the JVM's eyes, this
String S1 = "This was only a" + "simple" + "test"; The fact is:
String S1 = "This was only a simple test"; So of course it doesn't need too much time. But it is important to note that if your string is from another string object, the speed is not so fast, for example:
String S2 = "This was only a";
String S3 = "simple";
String S4 = "Test";
String S1 = S2 +s3 + S4;
At this point the JVM will behave in the same way as it did.
In most cases StringBuffer > String
StringBuffer
Java.lang.StringBuffer A variable sequence of characters for thread safety.
A string-like buffer that resembles string. But cannot be changed.
Although it includes a specific sequence of characters at random points in time. However, some method calls can change the length and content of the sequence.
String buffers can be safely used with multiple threads. Be able to synchronize these methods when necessary. As a result, all operations on a particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved.
The main operations on StringBuffer are the Append and insert methods. These methods can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer. The Append method always adds these characters to the end of the buffer. The Insert method adds a character to the specified point.
For example, assuming that Z refers to a string buffer object where the current content is "start", this method calls Z.append ("le") to include the string buffer with "startle". Z.insert (4, "le") will change the string buffer to include "starlet".
In most cases StringBuilder > StringBufferjava.lang.StringBuilde
Java.lang.StringBuilder A variable sequence of characters is 5.0 new. This class provides an API that is compatible with StringBuffer. However, synchronization is not guaranteed.
This class is designed for use with StringBuffer a simple replacement. When used in a string buffer used by a single thread (such cases are very common). Suppose possible. It is recommended to use this as a priority, due to the majority of implementations. Faster than StringBuffer. Two basically the same method.
String,stringbuffer and StringBuilder difference??