如果字符串连接使用 +是使用 StringBuilder 实现的,那么为什么在串联过程中会创建额外的对象呢?
如果以下代码:
String s = "a" + 1 + "b";// 1.
使用 using StringBuilder 实现相当于
String s = new StringBuilder().append("a").append(1).append("b");
那么会在 1 中创建额外的对象“a”和“b”,为什么?
If the following code:
String s = "a" + 1 + "b";// 1.
Is implemented using using StringBuilder equivalent to
String s = new StringBuilder().append("a").append(1).append("b");
then will extra objects "a" and "b" be created in 1 and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例实际上不会使用
StringBuilder
,因为所有元素都不是变量。因为“a”、1 和“b”都是文字,所以编译器将为您生成一个String
!但是,如果您在该String
连接中包含一个变量,那么它将使用StringBuilder
并且需要单独的String
来存储连接的元素。对于您的示例,编译器将创建单个字符串文字:
假设我们改为编写
现在编译器需要创建一个
StringBuilder
,并且它将使用 a 和 b 常量 ASCII 文字以及StringBuilder
。Your example will not actually use a
StringBuilder
because none of the elements are variables. Because "a", 1, and "b" are all literals, the compiler will make a singleString
for you! If, however, you included a variable in thatString
concatenation, then it would use aStringBuilder
and would need separateString
s for the concatenated elements.For your example the compiler would create a single String literal:
Let's say we had instead written
Now the compiler will need to create a
StringBuilder
, and it will use the a and b constant ASCII literals with theStringBuilder
.我假设您想知道为什么通常在字符串上使用
StringBuilder
而不是+
运算符更有效。在您的示例中,差异可以忽略不计,但您必须考虑更大的问题,通常是由循环引起的。以下面的代码为例:
不管你是否使用
StringBuilder
来优化这个+
操作,你都会创建一个String
对象来存储在s
中。因此,每次循环迭代都会创建一个新的 String 对象,从而为您提供大量新创建的对象。相反,如果您在循环内使用带有
sb.append("something")
的StringBuilder
,则不需要StringBuilder
来创建每次循环迭代的String
对象。它可以自由地等待创建一个,直到循环后需要它为止。I assume you are wondering why it is more efficient in general to use
StringBuilder
instead of the+
operator on Strings. In your example, the difference is negligible, but you have to consider larger problems, usually resulting from looping.Take for example the following code:
No matter, if you use a
StringBuilder
to optimize this+
operation or not, you create aString
object to store ins
. So each loop iteration will create a newString
object, giving you a huge number of newly created objects.In contrast, if you use a
StringBuilder
withsb.append("something")
inside the loop, theStringBuilder
is not required to create aString
object for each loop iteration. It can freely wait to create one, until it is required after the loop.我认为你有点混淆了......
对于
+
-情况,在评估" 时会创建一个额外的
(就在将"a1"
-对象a" + 1"a1"
与"b"
连接之前)。使用append
方法可以避免这种额外的对象创建。就这样。"a"
和"b"
已在编译时创建。 (这些常量文字将从一开始就存在于字符串池中。)但您可能知道,这些常量也会在append
情况下创建。I think you're mixing it up a bit...
For the
+
-case, there will be an extra"a1"
-object created when evaluating"a" + 1
(right before concatenating"a1"
with"b"
). This extra object creation can be avoided when using theappend
method. Thats all.The
"a"
and"b"
will already be created during compile time. (Those constant literals will be present in the sting pool from the beginning.) But as you probably know, those constants will be created also in theappend
case.