java 在哪里使用 StringBuffer/StringBuilder 而不是 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11273823/
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-10-31 04:29:29  来源:igfitidea点击:

Where to use StringBuffer/StringBuilder than String

javaandroidstringstringbuilder

提问by Soumyadip Das

Possible Duplicate:
String, StringBuffer, and StringBuilder

可能的重复项:
String、StringBuffer 和 StringBuilder

We know that Stringare immutable where StringBuffer/StringBuilderare mutable. But sometimes we get confused what to use in our code.. the Stringor StringBuffer/StringBuilder?? Practically in our maximum code/quick code we use to prefer Stringthan StringBuffer/StringBuilder.

我们知道/是String不可变的。但有时我们会混淆在我们的代码中使用什么.. StringStringBuffer/StringBuilder?? 实际上在我们的最大码/快速的代码,我们使用喜欢比/ 。StringBufferStringBuilderStringStringBufferStringBuilder

This question is to solve the confusion, if you have any idea & proper reason for that, then please give a reply.

这个问题是为了解决困惑,如果您对此有任何想法和正当理由,请回复。

回答by user370305

Below is the main difference between these three most commonly used classes.

以下是这三个最常用的类之间的主要区别。

  • String class objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
  • StringBuffer is synchronized while StringBuilder is not synchronized.
  • Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.
  • String 类对象是不可变的,而 StringBuffer 和 StringBuilder 对象是可变的。
  • StringBuffer 是同步的,而 StringBuilder 是不同步的。
  • 连接运算符“+”是使用 StringBuffer 或 StringBuilder 在内部实现的。

Criteria to choose among String, StringBufferand StringBuilder

StringStringBufferStringBuilder 中选择的标准

  • If the Object value is not going to change use String Class because a String object is immutable.
  • If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
  • In case the Object value can change, and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.
  • 如果 Object 值不会更改,请使用 String Class,因为 String 对象是不可变的。
  • 如果 Object 值可以更改并且只能从单个线程访问,请使用 StringBuilder,因为 StringBuilder 是不同步的。
  • 如果 Object 值可以更改,并且会被多个线程修改,请使用 StringBuffer,因为 StringBuffer 是同步的。

回答by mishadoff

The main idea is that String is immutable. So when you are trying to modify it, new object created. StringBuffer and StringBuilder are mutable. And the difference is the first one is thread-safe.

主要思想是 String 是不可变的。因此,当您尝试修改它时,会创建新对象。StringBuffer 和 StringBuilder 是可变的。区别在于第一个是线程安全的。

The common approach to using StringBuilder is to parse something when you iteratively create a String object in a NON thread safe environment.

使用 StringBuilder 的常见方法是在非线程安全环境中迭代创建 String 对象时解析某些内容。

For example, you have a numbers array [1, 2, 3]. To create object "String1String2String3" you can use a StringBuilder

例如,您有一个数字数组 [1, 2, 3]。要创建对象“String1String2String3”,您可以使用 StringBuilder

StringBuilder builder = new StringBuilder();
foreach(Integer num : array) {
    builder.append("String").append(num);
}
builder.toString();

It's better than just using String string = string + ("String" + num);. AFAIK, compiler will optimize using string concatenation in the loop to StringBuilder, but better to use it manually.

这比仅使用String string = string + ("String" + num);. AFAIK,编译器将在循环中使用字符串连接优化到 StringBuilder,但最好手动使用它。

StringBuffer is used when you have shared states, that are modified by concurrent threads.

StringBuffer 在您有共享状态时使用,这些状态由并发线程修改。

回答by verisimilitude

StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. Thus, StringBuffer objects are generally safe to use in a multi-threaded environment.

StringBuffers 是线程安全的,这意味着它们具有同步方法来控制访问,因此一次只有一个线程可以访问 StringBuffer 对象的同步代码。因此,在多线程环境中使用 StringBuffer 对象通常是安全的。

StringBuilder's access is not synchronized so that it is not thread-safe. By not being synchronized, the performance of StringBuilder can be better than StringBuffer. Thus, if you are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance.

StringBuilder 的访问不是同步的,因此它不是线程安全的。通过不同步,StringBuilder 的性能可以优于 StringBuffer。因此,如果您在单线程环境中工作,使用 StringBuilder 而不是 StringBuffer 可能会提高性能。