Replace concat String to append in StringBuilder parameters

Ulf Zibis Ulf.Zibis at
Wed Aug 27 09:41:21 UTC 2014

Hi all,

a critical question!

1.) Why we have String concatenation in Java? ... I would answer: for _readability_ purpose.

2.) In the early javac times, we were asked to refactor to StringBuffer for performance critical code.

3.) Since 1.6 ? javac is capable to replace multiple concatenation with one Stringbuilder, so we are 
again asked to refactor the code for better readability.

4.) Now we see, that javac optimization fails again if StringBuilder, concatenation, toString(), 
append(String), append(Collection) etc. and StringJoiner use is mixed.

5.) Couldn't we force the better optimization of javac instead again refactoring the code and 
decrease readability again?

+ manual .append("x") should be translated to .append('x')
+ Javac could calculate a reasonable buffer size init value.


Am 11.08.2014 um 16:01 schrieb Ulf Zibis:
> Am 11.08.2014 um 15:12 schrieb Andrej Golovnin:
>> In the most classes I mentioned in my previous mail only the
>> #toString()-methods would be affected by the proposal. And in the most
>> cases, maybe in all cases, the #toString()-methods in this classes exists
>> only to provide nice output.
> So why not "nice input" from the java sources ...i.e.: use concatenation only if possible. The 
> performance problem occurs, if both strategies are mixed.
>> Btw. I see here a nice opportunity to create an RFE
>> for the Javac team. Following code:
>> Object o1 = ...;
>> Object o2 = ...;
>> String s = "abc" + o1 + "c" + o2 + "\n";
>> should be translated to:
>> String s = new
>> StringBuilder().append("abc").append(o1).append('c').append(o2).append('\n').toString();
>> instead of:
>> String s = new
>> StringBuilder().append("abc").append(o1).append("c").append(o2).append("\n").toString();
> + manual .append("x") should be translated to .append('x')
> + Javac could avoid to instantiate multiple SBs from mixed concatenation/SB source code.
> + Javac could calculate a reasonable buffer size init value.
> -Ulf

More information about the core-libs-dev mailing list