WHWW之String,StringBuffer,StringBuilder

What: What is the difference between String, StringBuffer, StringBuilder?

String is immutable string, thread safe

StringBuffer is mutable string, thread safe

StringBuilder is a mutable string and is not thread safe.

In most cases, the speed of string concatenation is: StringBuilder>StringBuffer>String

How: how to achieve it?

public class Test1 {

    public static void main(String[] args) {
        String string="String";
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append(string+"Buffer");
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(string+"Builder");
System.out.println("String:"+string);
System.out.println("StringBuffer:"+stringBuffer);
System.out.println("StringBuilder:"+stringBuilder);
}
                
                
                        
    
}

 How: How to choose to use String, StringBuffer, StringBuilder?

1. If you want to operate a small amount of data, use String

2. Operate a large amount of data under the single-threaded string buffer StringBuilder

3. Multi-threaded operation of the string buffer under the operation of a large amount of data StringBuffer

Why: Since there is String, why introduce StringBuffer and StringBuilder?

To improve the speed of string splicing: Because the speed of string splicing is: StringBuilder>StringBuffer>String

Why: Why is the string splicing speed: StringBuilder>StringBuffer>String?

Because String is immutable, every time its variable value is changed, a new object is regenerated, and the variable reference is just pointed to the new object, so the speed is relatively slow.

The operation of StringBuffer is actually a direct operation of the reference pointed to by the object, without generating a new object, the speed is very fast, and it is thread-safe.

StringBuilder is newly added after jdk5, and its usage is the same as StringBuffer, but it is thread-unsafe, so it is the fastest in single thread.

Why: Why is String immutable?

Looking at the String source code, you can know that the String value is actually a char[] array, which is modified by final and set to private. Fields modified by final are immutable.

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
private final char value[];
    

Note: The immutability of char[]value here means that the reference of value cannot be changed, but the value of the array itself can be changed

And the whole String is set to final, so String is immutable.

Why: Why is String made final?

Because final-modified classes cannot be inherited, it is necessary to refuse inheritance and prevent them from being destroyed.

why: why not apply StringBuffer, StringBuilder to HashMap, HashSet?

public class Test2 {
    public static void main(String[] args) {
        HashSet<StringBuffer> hashSet=new HashSet<>();
        StringBuffer stringBuffer=new StringBuffer("String");
        StringBuffer stringBuffer1=new StringBuffer("StringBuffer");
        hashSet.add(stringBuffer);
        hashSet.add(stringBuffer1);
        System.out.println("第一次输出:"+hashSet);
        StringBuffer stringBuffer2=stringBuffer;
        stringBuffer2.append("Builder");
        System.out.println("第二次输出:"+hashSet);
    }
}

Test Results:

This violates the uniqueness of HashSet.

Where: Where are String, StringBuffer, StringBuilder stored?

String is stored in the constant pool, which has been determined at compile time. ( The constant pool is actually a memory space, different from the heap space where the objects created with the new keyword are located. ) new String() is not a string constant, it has its own address space, which is stored in the heap ( new objects, arrays are placed on the heap ); StringBuffer, StringBuilder are stored in the heap space.

where: where is the benefit of making String immutable?

1. Because String is in the constant pool, immutability can save memory space and improve efficiency

2. In the case of multiple concurrency, due to immutability, its object cannot be rewritten, and it is thread-safe.

3. Just for safety

My motto: No, I can learn; I fall behind, I can catch up; I fall, I can stand up; I will do it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847947&siteId=291194637