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
Difference between StringBuilder and StringBuffer
提问by blacktiger
What is the main difference between StringBuffer
and StringBuilder
?
Is there any performance issues when deciding on any one of these?
StringBuffer
和之间的主要区别是StringBuilder
什么?在决定其中任何一项时是否有任何性能问题?
采纳答案by sblundy
StringBuffer
is synchronized, StringBuilder
is not.
StringBuffer
是同步的,StringBuilder
不是。
回答by OscarRyz
Basically, StringBuffer
methods are synchronized while StringBuilder
are 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:
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 Vector
and ArrayList
.
同样的事情发生Vector
和ArrayList
。
回答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
StringBuffer
without 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
StringBuffer
is synchronized, but StringBuilder
is not. As a result, StringBuilder
is faster than StringBuffer
.
StringBuffer
是同步的,但StringBuilder
不是。结果,StringBuilder
比 快StringBuffer
。
回答by polygenelubricants
StringBuilder
is faster than StringBuffer
because it's not synchronized
.
StringBuilder
比StringBuffer
因为它不是更快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 ms
for StringBuffer
vs 753 ms
for StringBuilder
.
一个试运行给出的数字2241 ms
为StringBuffer
VS753 ms
的StringBuilder
。
回答by Ketan Patel
A String
is an immutable object which means the value cannot be changedwhereas StringBuffer
is mutable.
AString
是一个不可变的对象,这意味着它的值不能改变而是StringBuffer
可变的。
The StringBuffer
is Synchronized hence thread-safe whereas StringBuilder
is not and suitable for only single-threaded instances.
的StringBuffer
同步,因此线程安全而StringBuilder
不是与适于仅单线程实例。
回答by subodh ray
StringBuilder
and StringBuffer
are almost the same. The difference is that StringBuffer
is synchronized and StringBuilder
is not. Although, StringBuilder
is faster than StringBuffer
, the difference in performance is very little. StringBuilder
is 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
.