Simple comparison about String, StringBuilder and StringBuffer

To know the difference between the three, let's take a look at some of these methods:
First, String:

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

String maintains a char type character array and uses the final keyword to modify it, so the String object is immutable.Let's
take a look at StringBuilder:

public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{

    /** use serialVersionUID for interoperability */
    static final long serialVersionUID = 4383685877147921099L;

    public StringBuilder() {
        super(16);
    }

    public StringBuilder(int capacity) {
        super(capacity);
    }

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

    public StringBuilder(CharSequence seq) {
        this(seq.length() + 16);
        append(seq);
    }
    ......

StringBuilder inherits AbstractStringBuilder, all its construction methods are from AbstractStringBuilder, and then look at the AbstractStringBuilder class:

abstract class AbstractStringBuilder implements Appendable, CharSequence {
    char[] value;//没有用final修饰
    int count;
    AbstractStringBuilder() {
    }

    /**
     * 创建指定长度的char数组
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }
......

As can be seen from the above code, char [] value is not modified with the final keyword, so the StringBuilder object is variable. The
Insert picture description heresame is true when viewing StringBuffer. Its construction method is the same as StringBuilder, indicating that StringBuffer is variable Object, but its other methods are decorated with the synchronized keyword, that is to say, StringBuffer uses a mechanism with synchronization lock to ensure thread safety, StringBuilder method does not have this keyword, so StringBuilder can not be used for more Thread environment.

......
 public synchronized StringBuffer append(StringBuffer sb) {
        toStringCache = null;
        super.append(sb);
        return this;
    }

    /**
     * @since 1.8
     */
    @Override
    synchronized StringBuffer append(AbstractStringBuilder asb) {
        toStringCache = null;
        super.append(asb);
        return this;
    }
    ......

Now summarize the String, StringBuilder and StringBuffer from the following 3 aspects:

  • Mutability:
    String objects are immutable, StringBuilder and StringBuffer are mutable objects, because String objects use final keywords to modify character arrays to save strings, while StringBuilder and StringBuffer do not have final keywords.
  • Thread safety
    String objects are immutable, which can be understood as constants, and there is no doubt that thread safety;
    StringBuffer is a variable object, but the method adds the synchronized keyword to ensure synchronization, so thread safety;
    StringBuilder thread is not safe;
  • Performance
    (usually): StringBuilder> StringBuffer> String
    Because String is immutable, every time you change the String type, a new String object will be generated in the heap memory, and then the pointer will point to the new String object, the old String objects are collected by the JVM's garbage collection mechanism (GC). Both StringBuffer and StringBuilder operate on the object itself, and return this (that is, this type of object) after each append (). Because StringBuffer uses java's synchronization lock mechanism, its operation is slower than StringBuilder.

to sum up:

  1. Operate a small amount of data: apply String;
  2. Operate a large amount of data under the single-thread operation string buffer: apply StringBuilder;
  3. Multi-thread operation operates a large amount of data under the string buffer: StringBuffer is applicable.
Published 39 original articles · won praise 1 · views 4620

Guess you like

Origin blog.csdn.net/thetimelyrain/article/details/99895969