Java StringBuilder 和 StringBuffer 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/355089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:45:15  来源:igfitidea点击:

Difference between StringBuilder and StringBuffer

javastringbuilderstringbuffer

提问by blacktiger

What is the main difference between StringBufferand StringBuilder? Is there any performance issues when deciding on any one of these?

StringBuffer和之间的主要区别是StringBuilder什么?在决定其中任何一项时是否有任何性能问题?

采纳答案by sblundy

StringBufferis synchronized, StringBuilderis not.

StringBuffer是同步的,StringBuilder不是。

回答by OscarRyz

Basically, StringBuffermethods are synchronized while StringBuilderare not.

基本上,StringBuffer方法是同步的,而StringBuilder不是同步的。

The operations are "almost" the same, but using synchronized methods in a single thread is overkill.

操作“几乎”相同,但在单个线程中使用同步方法是多余的。

That's pretty much about it.

差不多就是这样。

Quote from StringBuilder API:

引自StringBuilder API

This class [StringBuilder] provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

此类 [StringBuilder] 提供了与 StringBuffer 兼容的 API,但不保证同步。此类旨在用作 StringBuffer 的替代品,用于在单个线程使用字符串缓冲区的地方(通常是这种情况)。在可能的情况下,建议优先使用此类而不是 StringBuffer,因为在大多数实现下它会更快。

So it was made to substitute it.

所以它被用来代替它。

The same happened with Vectorand ArrayList.

同样的事情发生VectorArrayList

回答by Marc Novakowski

StringBuilder was introduced in Java 1.5 so it won't work with earlier JVMs.

StringBuilder 是在 Java 1.5 中引入的,因此它不适用于早期的 JVM。

From the Javadocs:

Javadocs

StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

StringBuilder 类提供与 StringBuffer 兼容的 API,但不保证同步。此类旨在用作 StringBuffer 的替代品,用于在单个线程使用字符串缓冲区的地方(通常是这种情况)。在可能的情况下,建议优先使用此类而不是 StringBuffer,因为在大多数实现下它会更快。

回答by Learning

StringBuilder is not thread safe. String Buffer is. More info here.

StringBuilder 不是线程安全的。字符串缓冲区是。更多信息在这里

EDIT: As for performance , after hotspotkicks in , StringBuilder is the winner. However , for small iterations , the performance difference is negligible.

编辑:至于性能,热点开始后,StringBuilder 是赢家。但是,对于小迭代,性能差异可以忽略不计。

回答by JRomio

StringBuffer

StringBuffer

  • Synchronized hence threadsafe
  • Thread safe hence slow
  • 同步因此线程安全
  • 线程安全因此缓慢

StringBuilder

StringBuilder

  • Introduced in Java 5.0
  • Asynchronous hence fast & efficient
  • User explicitly needs to synchronize it, if he wants
  • You can replace it with StringBufferwithout any other change
  • 在 Java 5.0 中引入
  • 异步因此快速高效
  • 用户明确需要同步它,如果他愿意
  • 您可以将其替换为StringBuffer无需任何其他更改

回答by JRomio

StringBuffer is used to store character strings that will be changed (String objects cannot be changed). It automatically expands as needed. Related classes: String, CharSequence.

StringBuffer 用于存储将要更改的字符串(String 对象不能更改)。它会根据需要自动扩展。相关类:String、CharSequence。

StringBuilder was added in Java 5. It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster.

StringBuilder 是在 Java 5 中添加的。它在所有方面都与 StringBuffer 相同,只是它不同步,这意味着如果多个线程同时访问它,可能会出现问题。对于单线程程序,最常见的情况是,避免同步开销会使 StringBuilder 稍微快一点。

回答by Ahmad adawi

StringBufferis synchronized, but StringBuilderis not. As a result, StringBuilderis faster than StringBuffer.

StringBuffer是同步的,但StringBuilder不是。结果,StringBuilder比 快StringBuffer

回答by polygenelubricants

StringBuilderis faster than StringBufferbecause it's not synchronized.

StringBuilderStringBuffer因为它不是更快synchronized

Here's a simple benchmark test:

这是一个简单的基准测试:

public class Main {
    public static void main(String[] args) {
        int N = 77777777;
        long t;

        {
            StringBuffer sb = new StringBuffer();
            t = System.currentTimeMillis();
            for (int i = N; i --> 0 ;) {
                sb.append("");
            }
            System.out.println(System.currentTimeMillis() - t);
        }

        {
            StringBuilder sb = new StringBuilder();
            t = System.currentTimeMillis();
            for (int i = N; i > 0 ; i--) {
                sb.append("");
            }
            System.out.println(System.currentTimeMillis() - t);
        }
    }
}

A test rungives the numbers of 2241 msfor StringBuffervs 753 msfor StringBuilder.

一个试运行给出的数字2241 msStringBufferVS753 msStringBuilder

回答by Ketan Patel

A Stringis an immutable object which means the value cannot be changedwhereas StringBufferis mutable.

AString是一个不可变的对象,这意味着它的值不能改变而是StringBuffer可变的。

The StringBufferis Synchronized hence thread-safe whereas StringBuilderis not and suitable for only single-threaded instances.

StringBuffer同步,因此线程安全而StringBuilder不是与适于仅单线程实例。

回答by subodh ray

StringBuilderand StringBufferare almost the same. The difference is that StringBufferis synchronized and StringBuilderis not. Although, StringBuilderis faster than StringBuffer, the difference in performance is very little. StringBuilderis a SUN's replacement of StringBuffer. It just avoids synchronization from all the public methods. Rather than that, their functionality is the same.

StringBuilder并且StringBuffer几乎相同。区别在于StringBuffer同步和StringBuilder不同步。虽然,StringBuilder比 快StringBuffer,但性能差异很小。StringBuilder是 SUN 的替代品StringBuffer。它只是避免了所有公共方法的同步。相反,它们的功能是相同的。

Example of good usage:

良好用法示例:

If your text is going to change and is used by multiple threads, then it is better to use StringBuffer. If your text is going to change but is used by a single thread, then use StringBuilder.

如果您的文本将要更改并被多个线程使用,那么最好使用StringBuffer. 如果您的文本要更改但被单个线程使用,请使用StringBuilder.