标签云

微信群

扫码加入我们

WeChat QR Code


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

2019年01月23日45分16秒

I'm not sure + can be decompiled.

1970年01月01日00分03秒

Use javap to disassemble a Java class file.

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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.)

2019年01月23日45分16秒

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.

2019年01月23日45分16秒

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.

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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]?

2019年01月22日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

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.

2019年01月23日45分16秒

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()

2019年01月23日45分16秒

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.

2019年01月22日45分16秒

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

2019年01月23日45分16秒

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

2019年01月23日45分16秒

using the + operator is equivalent to using the StringBuilder (docs.oracle.com/javase/specs/jls/se8/html/…)

2019年01月23日45分16秒