StringBuffer and StringBuilder are both mutable strings, but the difference between them is one of the most likely questions to come up in Java early and intermediate interviews. Such a simple question, stack long in the recent interview process, but often encountered a lot of job seekers say the opposite, do not know the use of the situation.

Today I’m going to take you through some of the differences between StringBuffer and StringBuilder and how they can be used. If you’re a StringBuffer, please skip them. If you’re new to StringBuffer, or if you’re not familiar with them, you can learn about them and prepare for your interview at the end of the year.

Take a look at the class structure of StringBuffer and StringBuilder:

AbstractStringBuilder > AbstractStringBuilder > AbstractStringBuilder > AbstractStringBuilder > AbstractStringBuilder > AbstractStringBuilder > AbstractStringBuilder Let’s look at the three differences.

Difference 1: Thread safety

StringBuffer: thread safe, StringBuilder: thread unsafe. Because all of StringBuffer’s exposed methods are synchronized modified, StringBuilder doesn’t have a StringBuilder modifier.

StringBuffer snippet:

@Override
public synchronized StringBuffer append(String str) {
    toStringCache = null;
    super.append(str);
    return this;
}
Copy the code

Difference 2: Buffers

StringBuffer snippet:

private transient char[] toStringCache;

@Override
public synchronized String toString() {
    if (toStringCache == null) {
        toStringCache = Arrays.copyOfRange(value, 0, count);
    }
    return new String(toStringCache, true);
}
Copy the code

StringBuilder snippet:

@Override
public String toString() {
    // Create a copy, don't share the array return new String(value, 0, count); }Copy the code

As you can see, StringBuffer uses the toStringCache value of the cache directly to construct a string each time it fetches toString.

StringBuilder, on the other hand, copies the character array and constructs a string each time.

So, caching is also an optimization for StringBuffer, but the toString method of StringBuffer is still synchronized.

Difference 3: Performance

Since StringBuffer is thread-safe and all of its exposed methods are synchronized, StringBuilder does not lock methods, so there is no doubt that StringBuilder performs much better than StringBuffer.

conclusion

So StringBuffer is best used in situations where multiple threads operate on the same StringBuffer, and StringBuilder is best used in single-threaded situations.

This is for the interview or will go to the interview of the Java program ape, if it is helpful to you, also welcome to share with friends around, let everyone less detours.

Follow the wechat public account of Java technology stack, stack leader will continue to share Java dry goods tutorial, the public account will be the first time to push, continue to pay attention to. In the public number background reply: Java, get stack length finishing more Java tutorial, are actual combat dry goods.