In Java project development, strings are the longest used data type, and string, StringBuilder, and stringbuffer of strings often make it unclear when they should be used.
I hereby tidy it up.
string literal constant
As you all know, string is an immutable class, and any change to string will cause a new string object to be generated, so each time a change is made to the string type it is 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 to change the content of the strings, because each generation of the object will have an impact on the 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.
stringbuffer string variable (thread safe)To compensate for the immutable disadvantages of the string class, StringBuffer and StringBuilder classes are available. These two classes are mutable, except that one is thread-safe and one is non-thread safe. In general, we recommend the use of StringBuffer, especially if the string object is constantly changing. In some special cases, string object concatenation is actually interpreted by the JVM as a concatenation of StringBuffer objects, so the speed of a string object is not slower than the StringBuffer object, especially in the following string object generation, Stri NG efficiency is far faster than StringBuffer:
String S1 = "This was only a" + "simple" +"Test"; StringBuffer Sb=NewStringBuilder ("This was only a"). Append ("simple"). Append ("test");//you'll be surprised to find that the speed at which the String S1 object is generated is simply too fast, and at this point the stringbuffer is not at all dominant at all. In fact, this is a JVM trick, in the JVM's eyes, thisString S1 = "This was only a" + "simple" +"Test";//is actually:String S1 ="This was only a simple test";//so of course it doesn't take 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 .
StringBuilder string variable (non-thread safe)
StringBuilder a variable sequence of characters is 5.0 new. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer. The two methods are basically the same.
Summary
If our program runs on a single thread, or does not have to take into account thread synchronization issues, we should prioritize the use of the StringBuilder class, and of course, if you want to ensure thread safety, natural non-stringbuffer.
The difference between string, StringBuilder, StringBuffer in Java