Comparison of String, StringBuilder, StringBuffer

Often we are not clear about the distinction between Sting/stringBuffer/StringBuilder, so write a blog to summarize:

The value of String is unmodifiable, and each operation on String will generate a new object, wasting a lot of space;
Stringbuffer value can be changed, efficiency is general, thread-safe
StringBuilder value can be changed, efficiency is high, thread is not safe

1. The variability of the three values

String is immutable because it is actually a character array at the bottom of String, which is of fixed length.
Take a look at the original code of String:

/** The value is used for character storage. */
    private final char value[];

 public String(char value[]) {
    
    
        this.value = Arrays.copyOf(value, value.length);
    }

It can be seen that the length is specified according to the length of the defined string, so there is no way to change its length. Operating on String will generate a new object, which is also a new array. Imagine if you use String to make changes in a loop, it wastes a lot of space.
Let's take a look at the source code of StringBuilder and StringBuffer:

public StringBuilder(String str) {
    
    
        super(str.length() + 16);
        append(str);
    }
 public StringBuffer(String str) {
    
    
        super(str.length() + 16);
        append(str);
    }

It can be seen that the values ​​of Stringbuffer and StringBuilder can be changed.

2. The inheritance relationship between String, StringBuffer and StringBuilder

Insert picture description here
We can observe the inheritance relationship of the three from the source code of the three.

3. The efficiency of String, StringBuffer, Stringbuilder

First of all, stringbuilder was created after StringBuilder. If thread safety is not mentioned, StringBuilder is of course the fastest.
Then let's take a look at String.

String str="hello"+"world";//代码1
String s1="hello";
String s2="world";
String s12=s1+s2;//代码2
StringBuilder sb=new StringBuilder(s1);
sb.append(s2);//代码3

We observe the above code: Code 1 is constant + operation, code 2 is variable + operation;
Code 1: In fact, the code "hello" + "world" is connected during the compilation phase and points to the string pair in the heap Like it, when it runs, take it out of the pile. Stored in the local variable str. It seems to be a pick-and-place operation.
Code 2: When String uses + operation, it is actually divided into three steps:
(1) Stringbuilder temp=new StringBuilder(s1);
(2) temp.append(s2);
(3) str=temp.toString( );
Just a + sign does these three steps, and creates the String object and Stringbuilder object at the same time. If it is looped tens of thousands of times, tens of thousands of two objects will be created. Isn't it a waste of time and space?
Code 3: What is done at the bottom layer is to continuously expand the length of the character array, and then paste the new content behind the original array. Just did the expansion and things.
Summary: Stringbuilder and Stringbuffer have the highest efficiency in using append.

4. Security issues of String, StringBuffer, StringBuilder

Why is Stringbuilder thread unsafe, while stringbuffer is thread safe? We can look at the source code of both. Many methods in Stringbuffer are modified by Synchronized. As we all know, the Synchronized modification is added to the method, that is, a lock is added. When an object obtains the lock, the operation in the method can be performed. At this time, other threads cannot obtain the lock. Operation will not cause unsafe phenomena. Of course, no one will grab the lock if it is single-threaded, and Stringbuffer is as safe as Stringbuilder.
As for String, its value cannot be changed. If it is changed, a new object will be generated. Then it must be safe.

Guess you like

Origin blog.csdn.net/weixin_43815275/article/details/113846296