String is not a simple type, but a class that is used to represent a sequence of characters. The characters themselves comply with Unicode standards, and there are two ways to initialize them.
For example: String greeting="Good Morning! /n";
String greeting=new String(="Good Morning! /n");
The characteristic of String is that once assigned, the character object it points to cannot be changed. If changed, it will point to a new character object.
StringBuffer is a string object with the characteristics of object reference passing.
The StringBuffer object can call its methods to dynamically increase, insert, modify and delete operations, and does not need to specify the size in advance like an array, so as to achieve the effect of inserting characters multiple times and taking them out at one time, so it is very flexible and convenient to operate strings.
Once the final desired string is generated through StringBuffer, it can be called toString method to convert it into a String object.
The position of these two classes in string processing is self-evident. So what are their advantages and disadvantages and when should they use whom? Let's explain it from the following points
1. Comparison of the two in terms of execution speed: StringBuffer > String
2.String <StringBuffer Reason String: String constant StringBuffer: Character creation variable From the name above, String is a "character creation constant", that is, an unchangeable object. You may have a question about understanding this sentence, such as this code:
String s = "abcd"; s = s+1; System.out.print(s);// result : abcd1
We obviously changed the String variable s, why did it say that it has not changed? In fact, this is a kind of fraud. The JVM parses this code like this: first create object s, give an abcd, and then create a new one Object s is used to execute the second line of code, which means that object s has not changed before, so we say that the String type is an unchangeable object. Due to this mechanism, whenever a string is operated with String, it is actually New objects are constantly created, and the original objects will become garbage and be collected by GC. It can be imagined how effective this execution will be.
But StringBuffers are different. They are string variables and are mutable objects. Whenever we use them to operate on strings, we actually operate on an object, so that we will not create some things like String And if an external object operates, of course the speed will be faster
3. A special example:
The code copy is as follows:
String str = "This is only a" + "simple" + "test";
StringBuffer builder = new StringBuilder("This is only a").append("simple").append("test");
You will be surprised to find that the speed of generating str objects is simply too fast, and at this time, StringBuffer does not have any advantage in speed at all. Actually, this is a trick of the JVM, in fact:
String str = "This is only a" + "simple" + "test";
Actually:
String str = “This is only a simple test”;
So it doesn't take much time. But what you should note here is that if your string comes from another String object, the speed is not that fast, for example:
String str2 = "This is only a"; String str3 = "simple"; String str4 = "test"; String str1 = str2 +str3 + str4;
At this time, the JVM will do it in a regular manner in the original way.
4.StringBuilder and StringBuffer
StringBuilder: StringBuffer that is not secure: thread-safe When we are used by multiple threads in the string buffer, the JVM cannot guarantee that the operation of StringBuilder is safe. Although it has the fastest speed, it can ensure that StringBuffer can operate correctly. of. Of course, in most cases, we operate under a single thread, so in most cases, it is recommended to use StringBuilder instead of StringBuffer, which is the reason for the speed.
Summary of the use of the three:
1. If you want to operate a small amount of data, use = String
2. Operate a large amount of data in a single threaded string buffer = StringBuilder
3. Operate a large amount of data in a multi-threaded string buffer = StringBuffer