Talk about the difference between a String and a StringBuffer in Java

  • 2020-04-01 04:24:10
  • OfStack

String is not a simple type, but a class that is used to represent a sequence of characters. The character itself conforms to the Unicode standard and is initialized in two ways.

For example, String greeting= "Good Morning! \ n ";
String the greeting = new String (= "Good Morning! \ n ");
The thing about String is that once assigned, you can't change the character object it points to, and if you do, it points to a new character object.

A StringBuffer is a string object with the characteristics of object reference passing.

A StringBuffer object can call its methods to dynamically add, insert, modify, and delete characters without specifying the size in advance, as an array does, to achieve the effect of inserting characters multiple times and fetching them all at once, making it very flexible and convenient to manipulate strings.
Once the desired String is generated through a StringBuffer, its toString method can be called to convert it to a String object

There is no doubt about the positions of these two classes in string handling, so what are their strengths and weaknesses, and who should be used when? Let's explain from the following points

1. Comparison of the two in terms of execution speed: StringBuffer  >   The String

2. The String < The cause of the StringBuffer
String: String constant
StringBuffer: character creation variable
As you can see from the name above, a String is a "character creation constant," or an immutable object. You may have a question about how this sentence is interpreted   , such as this code:


String s = "abcd";  
s = s+1;  
System.out.print(s);// result : abcd1  

We just changed the String variable s, why not ? In fact this is a kind of deception, the JVM is parsing the code like this: first create the object s, gives a abcd, and then create a new object s used to perform the second line of code, that is to say, we did not change before the object s, so we said the type String is immutable object, because this mechanism, when operated by String String, actually continuously create new objects, and the original object will become garbage by GC recycles it, it is conceivable that there will be multiple execution efficiency.

Stringbuffers are different, they're String variables, they're mutable objects, and every time we do something with them on a String, we're actually doing it on an object, so we don't have to create something outside of it like a String, which is faster

3. A special case:  


String str = " This is only a " + " simple " + " test " ;   
StringBuffer builder = new StringBuilder( " This is only a " ).append( " simple " ).append( " test " );  

You'll be surprised to find that the speed at which STR objects are generated is simply too fast, and that StringBuffer is not at all superior in speed. This is a trick of the JVM, in fact:


    String str =  " This is only a "  +  "  simple "  +  " test " ; 


Which is:


String str =  " This is only a simple test " ; 

So it doesn't take much time. But the important thing to note here is that if your String comes from another String object, it's not as fast, like:


    String str2 =  " This is only a " ; 
    String str3 =  "  simple " ; 
    String str4 =  "  test " ; 
    String str1 = str2 +str3 + str4; 

At this point, the JVM will behave the way it should.

4. The StringBuilder and StringBuffer

StringBuilder: threads are not safe
StringBuffer: thread-safe
When we are using multiple threads in the StringBuffer, the JVM cannot guarantee that the StringBuilder's operation is safe, although it is the fastest, but it can guarantee that the StringBuffer can operate correctly. Of course, most of the time we're doing this in a single thread, so most of the time it's recommended to use StringBuilder instead of StringBuffer, just for speed.

Summary of the use of the three:

1. If you want to manipulate a small amount of data, use = String
2. Manipulate a large amount of data in a single threaded operation string buffer = StringBuilder
3. Multithreaded manipulation of large amount of data in StringBuffer = StringBuffer


Related articles: