在 Java 中,我什么时候应该更喜欢 String 而不是 StringBuilder,反之亦然?

In Java, when should I prefer String over StringBuilder vice versa?

我知道 StringBuilder 应该比 String 更受青睐,因为 String 将保存在常量字符串池中并且为其分配新值不会覆盖以前的值。但是,StringBuilder 是一个覆盖其先前值的对象。

对于固定的文本值,我会使用字符串。

在创建较大的文本字符串时使用 StringBuilder,例如:

final StringBuilder sb = new StringBuilder();
for(;;){
    sb.append("more text\n");//add text, StringBuilder's size will increase incrementally
    if(this.stop()){
         break;//stop loop
    }
}
final String result = sb.toString();// Create the final result to work with
System.out.println(result);//print result

使用 StringBuffer 而不是 StringBuilder 来获取同步值, 请参阅 了解 StringBuilder 和 StringBuffer 之间的区别。

JavaDoc: StringBuffer (http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html):

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls

JavaDoc: StringBuilder (http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html):

A mutable sequence of characters. This 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.

JavaDoc: 字符串 (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html):

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared

基本上,您将使用字符串作为文本常量(不可变)。

当你必须对 characters.As 的字符串进行大量修改时应该使用 java.lang.StringBuilder 类 我们知道 String 对象是不可变的,所以如果你选择做一个对 String 对象进行大量操作,最终会在 String 池中得到大量废弃的 String 对象。 (即使在 RAM 达到千兆字节的今天,将宝贵的内存浪费在丢弃的 String 池对象上也不是一个好主意。)另一方面,StringBuilder 类型的对象可以一遍又一遍地修改,而不会留下大量丢弃的对象字符串对象。

String x = "abc";
x.concat("def");
System.out.println("x = " + x); // output is "x = abc"


StringBuffer sb = new StringBuffer("abc");
sb.append("def");
System.out.println("sb = " + sb); // output is "sb = abcdef"

字符串是不可变对象,一旦创建就不能更改。创建为字符串的对象存储在常量字符串池中。 Java 中的每个不可变对象都是线程安全的,这意味着 String 也是线程安全的。 字符串不能被两个线程使用simultaneously.String一旦分配就不能改变。

String  demo = "Test1" ;

上述对象存储在常量字符串池中,其值不可修改

demo="Test2" ;

new "Test2" 字符串在常量池中创建并被演示变量引用

StringBuilder 对象是可变的,我们可以更改对象中存储的值。这实际上意味着,如果使用 StringBuilder 对象执行的字符串操作(例如追加)比 String 对象执行效率更高。

StringBuilder demo2= new StringBuilder("Test1");

上述对象也存储在堆中,其值可以修改

demo2=new StringBuilder("Test1"); 

上面的说法是正确的,因为它修改了StringBuilder中允许的值。

所以您应该在需要经常 update/modify 字符串的地方使用 stringBuilder。

一个简单的经验法则(String 是一种表示字符串的类型。StringBuilder 是可变字符流)

使用String表示文本值。根据定义 Java 提供字符串值的池化,从而为您提供一些 space 优化。在您的应用程序在文件批处理期间处理数百万个文本值的场景中考虑这一点。举个例子。

  String str1 = "Test";
  String str2 = "Test";

此处,str1 == str2(相同引用)

此外,+ 运算符在 String 中被重载以从不同类型构造 String。这可以在构造小字符串时使用(在内部使用 StringBuilder 完成,所以不用担心) - 但不能在循环时使用。

仅当您使用不同类型的小片段构造目标字符串时才使用 StringBuilder(或旧版本 StringBuffer)- 尤其是在循环中 - 这将帮助您避免在字符串池中放置不必要的字符串片段。

   StringBuilder s1 = new StringBuilder("test");
   StringBuilder s2 = new StringBuilder("test");

在这里,s1 != s2

另外,我不认为有什么方法可以操纵 StringBuilder/Buffer 的编码——同时 String 允许这样做。

编辑: 表示 Hibernate 实体: 始终使用 String 来表示 class 中的文本类型。出于上述原因,above.This 应该像肌肉记忆一样来找你。例如,原始类型为 intfloatchar 等,文本类型为 String。仅使用构建器来构建字符串而不是表示类型,除非这是一些奇怪的要求。

您应该使用 String,因为 String objects are cached in an object pool 如果不更改它们可能会提供更好的性能。

A StringBuilder 仅在您继续连接 String 标记时才有用,这在规范化良好的数据库中不应该是这种情况 table.

JVM 会进行各种优化,即使您使用串联,JVM 也可能会将该例程重写为 StringBuilder 版本。