Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

java - 字符串串联:concat()与“ +”运算符(String concatenation: concat() vs “+” operator)

Assuming String a and b:

(假设字符串a和b:)

a += b
a = a.concat(b)

Under the hood, are they the same thing?

(在幕后,他们是同一回事吗?)

Here is concat decompiled as reference.

(这里将concat反编译为参考。)

I'd like to be able to decompile the + operator as well to see what that does.

(我也希望能够反编译+运算符以查看其作用。)

public String concat(String s) {

    int i = s.length();
    if (i == 0) {
        return this;
    }
    else {
        char ac[] = new char[count + i];
        getChars(0, count, ac, 0);
        s.getChars(0, i, ac, count);
        return new String(0, count + i, ac);
    }
}
  ask by shsteimer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 .

(如果anull ,那么a.concat(b)引发一个NullPointerExceptiona+=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中提供了StringStringBuilder的源代码(及其程序包专用的基类)。)

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内部函数列表 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...