Recently learned to StringBuffer, the heart has a lot of questions, search some things about string,stringbuffer,stringbuilder, now tidy up.
The position of these three classes in string processing is self-evident, so what are their pros and cons and when should they be used? Here are some of the following explanations
1. Comparison of speed of implementation of the three: StringBuilder > StringBuffer > String
2.String < (Stringbuffer,stringbuilder) reasons
String: Strings constant
StringBuffer: Character creation variable
StringBuilder: Character creation variable
As you can see from the name above, string is a "character Fu Yi constant", which is an immutable object. For the understanding of this sentence you may have a question such as this code: 1 String s = "ABCD";
2 S = s + 1;
3 System.out.print (s); Result:abcd1
We obviously changed the variant s of the string, so why is that not changed? It's a deception, actually. The JVM parses this code by first creating the object s, giving an ABCD, and then creating a new object s to execute the second line of code, which means that we didn't change the object s before, so we said that the string type is an immutable object, because of this mechanism, When you manipulate strings with string, you are actually constantly creating new objects, and the original objects will become garbage collected by GC, so it is conceivable that the execution efficiency will be more than the end.
And StringBuffer and StringBuilder are not the same, they are string variables, are variable objects, and whenever we use them to manipulate strings, we actually operate on an object, so that we don't manipulate objects like string. Of course the speed is fast.
3. A special example: 1 String str = "This are only a" + "simple" + "test";
3 StringBuffer builder = new StringBuilder ("This is only a"). Append ("simple"). Append ("test");
You would be surprised to find that the Str object was generated at a rate that was simply too fast, and at this point the speed of the StringBuffer was not at all dominant. This is actually a trick of the JVM, actually:
String str = "This are only a" + "simple" + "test";
is actually:
String str = "This are simple test";
So it doesn't take too much time. But what you should be aware of here is that if your string is from another string object, the speed is not that fast, for example:
String str2 = "This are only a";
String STR3 = "simple";
String STR4 = "Test";
String str1 = str2 +str3 + str4;
This time the JVM will behave in the same way as it did.
4.StringBuilder and StringBuffer
StringBuilder: Thread is not safe
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 stringbuffer can be operated correctly. Of course, most of the time we are operating in a single thread, so in most cases it is recommended to use StringBuilder rather than stringbuffer, 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. Operation of a large amount of data under a single thread operation string buffer = StringBuilder
3. Multithreading operation string buffer operation large amount of data = StringBuffer
Reprint: http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html