I recently learned about stringbuffer, but I have some questions in my mind. I searched for something about string, stringbuffer, and stringbuilder. Now I want to sort it out.
The position of these three classes in string processing is self-evident. What are their advantages and disadvantages and who should they use? Below we will explain from the following points
1.Comparison of the three methods in execution speed:Stringbuilder> stringbuffer> string
2.String <(stringbuffer, stringbuilder)Cause
String: String constant
Stringbuffer: character-based Variable Creation
Stringbuilder: character variable Creation
From the above name, we can see that string is a "character to create a constant", that is, an unchangeable object. Your understanding of this sentence may lead to such 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 string-type variable S. Why is it not changed? In fact, this is a kind of spoofing. JVM parses this code like this: first create the object S, assign an ABCD, and then create a new object s to execute the second line of code, that is to say, our previous object s has not changed, so we say that the string type is an unchangeable object. Because of this mechanism, every time a string is used to manipulate a string, in fact, it is constantly creating new objects, and the original objects will become garbage collected by GC. it is conceivable that the execution efficiency will be much lower.
Stringbuffer and stringbuilder are different. They are string variables and changeable objects. Every time we use them to operate on strings, they are actually operated on an object, in this way, we will not create some external objects like strings for operations, but the speed will be faster.
3.A special example:
1 string STR = "this is only a" + "simple" + "test ";
3 stringbuffer builder = new stringbuilder ("This is only a"). append ("simple"). append ("test ");
You will be surprised to find that the STR object generation speed is too fast, and the stringbuffer speed is not dominant at all. In fact, this is a JVM trick. In fact:
String STR = "this is only a" + "simple" + "test ";
Actually:
String STR = "this is only a simple test ";
So it does not take much time. However, you should note that if your string is from another string object, the speed will not be so fast. For example:
String str2 = "this is only ";
String str3 = "simple ";
String str4 = "test ";
String str1 = str2 + str3 + str4;
At this time, the JVM will follow the original method in a regular manner.
4.Stringbuilder and stringbuffer
Stringbuilder: non-secure thread
Stringbuffer: thread-safe
When we use it in string buffering by multiple threads, the JVM cannot ensure that the stringbuilder operation is safe. Although its speed is the fastest, it can ensure that stringbuffer can be operated correctly. Of course, in most cases, we perform operations in a single thread. In most cases, we recommend using stringbuilder instead of stringbuffer, which is the reason for speed.
Summary of the three usage: 1. If you want to operate a small amount of data, use = string
2. A single thread operates on a large amount of data in the string buffer = stringbuilder
3. multi-threaded operations on a large amount of data in the string buffer = stringbuffer
I am a beginner, just summing up what I learned. It is inevitable that there are others in the writing. learning is a process of memory. These things are just for learning. Some things may be wrong, I hope you will give me some corrections.