No, not quite.
(不,不是。)
Firstly, there's a slight difference in semantics.
(首先,语义上略有不同。)
If a
is null
, then a.concat(b)
throws a NullPointerException
but a+=b
will treat the original value of a
as if it were null
. (如果a
是null
,那么a.concat(b)
引发一个NullPointerException
但a+=b
会治疗的原始值a
就好像它是null
。)
Furthermore, the concat()
method only accepts String
values while the +
operator will silently convert the argument to a String (using the toString()
method for objects). (此外, concat()
方法仅接受String
值,而+
运算符将无提示地将参数转换为String(对对象使用toString()
方法)。)
So the concat()
method is more strict in what it accepts. (因此, concat()
方法接受的条件更加严格。)
To look under the hood, write a simple class with a += b;
(若要深入了解,请编写一个带有a += b;
的简单类a += b;
)
public class Concat {
String cat(String a, String b) {
a += b;
return a;
}
}
Now disassemble with javap -c
(included in the Sun JDK).
(现在,使用javap -c
(Sun JDK中包含的)反汇编。)
You should see a listing including: (您应该会看到一个列表,其中包括:)
java.lang.String cat(java.lang.String, java.lang.String);
Code:
0: new #2; //class java/lang/StringBuilder
3: dup
4: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
7: aload_1
8: invokevirtual #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
11: aload_2
12: invokevirtual #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
15: invokevirtual #5; //Method java/lang/StringBuilder.toString:()Ljava/lang/ String;
18: astore_1
19: aload_1
20: areturn
So, a += b
is the equivalent of
(因此, a += b
等于)
a = new StringBuilder()
.append(a)
.append(b)
.toString();
The concat
method should be faster.
(concat
方法应该更快。)
However, with more strings the StringBuilder
method wins, at least in terms of performance. (但是,至少在性能方面,使用更多字符串时, StringBuilder
方法将获胜。)
The source code of String
and StringBuilder
(and its package-private base class) is available in src.zip of the Sun JDK.
(Sun JDK的src.zip中提供了String
和StringBuilder
的源代码(及其程序包专用的基类)。)
You can see that you are building up a char array (resizing as necessary) and then throwing it away when you create the final String
. (您会看到正在建立一个char数组(根据需要调整大小),然后在创建最终String
时将其丢弃。)
In practice memory allocation is surprisingly fast. (实际上,内存分配出奇地快。)
Update: As Pawel Adamski notes, performance has changed in more recent HotSpot.
(更新:正如Pawel Adamski指出的那样,在最近的HotSpot中,性能已经发生了变化。)
javac
still produces exactly the same code, but the bytecode compiler cheats. (javac
仍会产生完全相同的代码,但是字节码编译器会作弊。)
Simple testing entirely fails because the entire body of code is thrown away. (简单的测试完全失败,因为整个代码主体都被丢弃了。)
Summing System.identityHashCode
(not String.hashCode
) shows the StringBuffer
code has a slight advantage. (总结System.identityHashCode
(不是String.hashCode
)显示StringBuffer
代码具有一点优势。)
Subject to change when the next update is released, or if you use a different JVM. (在发布下一个更新时,或者使用其他JVM时,可能会发生更改。)
From @lukaseder , a list of HotSpot JVM intrinsics . (来自@lukaseder 的HotSpot JVM内部函数列表 。)