Recently learned to StringBuffer, the heart has a lot of questions, search some things about string,stringbuffer,stringbuilder, now tidy up a bit.
The position of the three classes in string processing is self-evident, so what are their pros and cons, and when should it be used? Let's take a look at the following points
1. Comparison of the three in execution speed:StringBuilder > StringBuffer > String
2. reason for String < (stringbuffer,stringbuilder)
String: Strings constant
StringBuffer: Character creation variable
StringBuilder: Character creation variable
As you can see from the above name, string is a "a constant", which is an immutable object. You may have a question about the understanding of this sentence, such as this code:
1 String s = "abcd";
2 S = s+1;
3 System.out.print (s); result:abcd1
We obviously changed the string type of the variable s, why is it not changed? In fact, this is a kind of deception, the JVM is parsing this code: first create the object s, give an ABCD, and then create a new object s used to execute the second line of code, that is, we have not changed the object s before, so we say that the string type is immutable object, because of this mechanism, Whenever you use string to manipulate strings, you are actually constantly creating new objects, and the original objects become garbage collected by GC, so it is conceivable that the execution will be more efficient.
And StringBuffer and StringBuilder are not the same, they are string variables, is a variable object, whenever we use them to operate on a string, it is actually operated on an object, so that it does not like a string to create a number of objects from the inside to operate, Of course the speed is fast.
3. A special example:
1 String str = "This was only a" + "simple" + "test";
3 stringbuffer Builder = new StringBuilder ("This was only a"). Append ("simple"). Append ("test");
You'll be surprised to find that the speed at which the Str object was generated was simply too fast, and at this time StringBuffer had no advantage at all. This is actually a trick of the JVM, in fact:
String str = "This was only a" + "simple" + "test";
is actually:
String str = "This was only a simple test";
So 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 str2 = "This was only a";
String STR3 = "simple";
String STR4 = "Test";
String str1 = str2 +str3 + str4;
At this point the JVM will behave in its original way.
4.StringBuilder and StringBuffer
StringBuilder: Thread is not secure
StringBuffer: Thread-safe
When we are using a string buffer to be used by multiple threads, the JVM does not guarantee that the StringBuilder operation is safe, although he is the fastest, but can ensure that the StringBuffer can be operated correctly. Of course, most of the time we are in a single-threaded operation, so in most cases it is recommended to use StringBuilder instead of StringBuffer, which is the reason for speed.
for a summary of the three uses :
1. Comparison of speed of execution: StringBuilder > stringbuffer
2. StringBuffer and StringBuilder, they are string variables, are objects that can be changed, and whenever we use them to manipulate strings, we actually operate on an object and do not create objects like string to manipulate them, so the speed is fast.
3. StringBuilder: Thread non-secure
StringBuffer: Thread-safe
When we are using a string buffer to be used by multiple threads, the JVM does not guarantee that the StringBuilder operation is safe, although he is the fastest, but can ensure that the StringBuffer can be operated correctly. Of course, most of the time we are in a single-threaded operation, so in most cases it is recommended to use StringBuilder instead of StringBuffer, which is the reason for speed.
Summary of the use of the three: 1. If you want to manipulate a small amount of data with = String
2. Manipulating large amounts of data under a single-threaded operation string buffer = StringBuilder
3. Multi-threaded operation string buffer operation large amount of data = StringBuffer
Go The difference between String, StringBuffer, and StringBuilder