Turn from "http://szh-java.iteye.com/blog/1666630"
--------------------------------------------------------------------------------------------------------
String
1,STIRNG is an object that is not a basic data type
2,string is the final class and cannot be inherited. is an immutable object that, once created, cannot modify its value.
3, for an already existing Stirng object, modify its value to recreate an object and assign the new value to the object
StringBuffer
1, a string-like buffer that modifies the object without re-creating it as a string.
2, use the Append () method to modify the value of the StringBuffer, and use the ToString () method to convert to a string.
Stringbuild
is a class used to replace StringBuffer after jdk1.5, and most of the time you can replace StringBuffer. And stringbuffer the difference is that Stringbuild is a single-threaded class, it is not worth performing thread synchronization, so faster than stringbuffer, high efficiency. is thread-insecure.
Turn from "http://www.cnblogs.com/A_ming/archive/2010/04/13/1711395.html"
----------------------------------------------------------------------------------------------------------
1. Comparison of the three in execution speed:StringBuilder > StringBuffer > String
2. reason for String < (stringbuffer,stringbuilder)
String: Strings constant
StringBuffer: String variable
StringBuilder: String variable
As you can see from the name above, string is a "string 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.
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
The difference between String, StringBuffer, and StringBuilder