标签云

微信群

扫码加入我们

WeChat QR Code

Assuming String a and b:

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

Under the hood, are they the same thing?

Here is concat decompiled as reference. 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);
    }
}


possible duplicate of StringBuilder vs String concatenation in toString() in Java

2018年08月15日49分35秒

I'm not sure + can be decompiled.

1970年01月01日00分03秒

Use javap to disassemble a Java class file.

2018年08月14日49分35秒

Due to 'immutability' you should probably be using StringBuffer or StringBuilder-(thread unsafe thus faster, instead

2018年08月14日49分35秒

HyperLink You can see the code using javap -c on a compiled class that uses it. (Oh, as in the answer. You just need to interpret the bytecode disassembly, which shouldn't be that difficult.)

2018年08月14日49分35秒

You can consult the JVM spec to understand the individual bytecodes. The stuff you'd want to reference is in chapter 6. A bit obscure, but you can get the gist of it fairly easily.

2018年08月14日49分35秒

I wonder why the Java compiler uses StringBuilder even when joining two strings? If String included static methods to concatenate up to four strings, or all the strings in a String[], code could append up to four strings with two object allocations (the result String and its backing char[], neither one redundant), and any number of strings with three allocations (the String[], the result String, and the backing char[], with only the first being redundant). As it is, using StringBuilder will at best require four allocations, and will require copying every character twice.

2018年08月15日49分35秒

That expression, a+=b. Doesn't it mean: a=a+b?

2018年08月15日49分35秒

Things have changed since when this answer was created. Please read my answer bellow.

2018年08月15日49分35秒

concat infact doesn't do that. I've edited my post with a decompilation of the concat method

2018年08月14日49分35秒

infact it does. Look a the first lines of your concat code. The problem with concat is that it always generates a new String()

2018年08月14日49分35秒

MarcioAguiar: maybe you mean that + always generates a new String - as you say, concat has one exception when you concat an empty String.

2018年08月14日49分35秒

Object creation time really matters. That's why in many situations we use StringBuilder directly rather than taking advantage of the StringBuilder behind +.

2018年08月14日49分35秒

coolcfan: When + is used for two strings, are there any cases where using StringBuilder is better than would be String.valueOf(s1).concat(s2)? Any idea why compilers wouldn't use the latter [or else omit the valueOf call in cases where s1 is known to be non-null]?

2018年08月14日49分35秒

supercat sorry I don't know. Maybe people who are behind this sugar are the best ones to answer this.

2018年08月14日49分35秒

I guess by "these temporary operations" you mean the use of escape analysis to allocate "heap" objects on the stack where provable correct. Although escape analysis is present in HotSpot (useful for removing some synchronisation), I don't believe it, is at the time of writing, u

2018年08月15日49分35秒

My dear, You know very well that any string literal treated as an String object itself which stores in String pool.So in this case we have 4 string literals .So obviously at least 4 objects should be created in pool.

2018年08月14日49分35秒

I don't think so: String s="I"+"am"+"good"+"boy"; String s2 = "go".concat("od"); System.out.println(s2 == s2.intern()); prints true, which means "good" was not in the string pool before calling intern()

2018年08月15日49分35秒

I am talking only about this line String s="I"+"am"+"good"+"boy"; In this case all 4 are string literals are kept in a pool.Hence 4 objects should be created in pool.

2018年08月14日49分35秒

"Currently + is implemented using StringBuffer" False It's StringBuilder. StringBuffer is the threadsafe impl of StringBuilder.

2018年08月15日49分35秒

It used to be StringBuffer prior to java 1.5, as that was the version when StringBuilder was first introduced.

2018年08月15日49分35秒