Convert StringBuilder to String concatenation suggestion

Answered

I have a block of code which creates a StringBuilder, then appends() several Strings (some of which are method parameters) before doing something with the resulting String.

IntelliJ is suggesting that this should be replaced by String concatenation, instead. Why?

As some of the Strings are method parameters, the compiler can't pre-concatenate the strings at compile time, so it'll just use a StringBuilder under the bonnet to do the job, anyway.

I can't see why turning a manual creation of a StringBuilder into an implicit creation is any better?

What am I missing?

9 comments
Comment actions Permalink

Could you please share a sample .java file to reproduce?

0
Comment actions Permalink
public class Main
{
private final static String STRING_1 = "1234567890";
private final static String STRING_2 = "0987654321";

public static void main(String args[])
{
doSomething(args[0]);
}

private static void doSomething(String s)
{
StringBuilder sb = new StringBuilder();

sb.append(STRING_1);
sb.append(s);
sb.append(STRING_2);

System.out.println(sb.toString());
}
}
0
Comment actions Permalink

In this basic case the efficiency would be the same. StringBuilder makes sense in a loop.

The compiled code will look like this:

String sb = "1234567890" + s + "0987654321";
1
Comment actions Permalink

Which the compiler will turn into:

new StringBuilder().append("1234567890").append(s).append("0987654321").toString();

Which, semantically, is no different to the original code.

0
Comment actions Permalink

True, but

String sb = STRING_1 + s + STRING_2;

is easier to read in the source code. If there is no difference in the performance and in the bytecode, the code which is easier to read wins, hence the suggestion.

0
Comment actions Permalink

The final comment here is wrong for two reasons

 

1. Whether you think that is more readable than the .append version is a matter of style and opinion and not fact.

2. When I see suggestions in this area, they are in the category of performance, not style / readability. In the case I am looking at, the new StringBuilder(n) with appends is more performant than the equivalent Java compiled version because I know the size of n. 

 

Chris

1
Comment actions Permalink

Hello Chris,

Could you please provide code example where IDE suggests to convert StringBuilder to concatenation and it decreases performance?

 

0
Comment actions Permalink

I am somewhat inclined to agree with Chris that when IntelliJ suggests under the heading of 'Performance' that we should replace an explicit StringBuilder use with an implicit StringBuilder use, that's not a performance concern.

Ideally IntelliJ would separate the ones that do have a performance impact (which I would leave on) from the ones that don't (which I might turn off).

Mostly, this suggestion just came up for me and I'm basically happy with the explicit version and if I'm not getting a performance benefit from switching it, I don't see the need, so I wanted to quickly check why IntelliJ was recommending it, which led me here.

0
Comment actions Permalink

Hello Geoffrey, 

Inspection 'StringBuilder' can be replaced with 'String' only reports the cases when the resulting concatenation is at least as efficient or more efficient than the original code. Replacing the StringBuilder with a String would be usually just as efficient, but the code would be more clear and readable. 

Please feel free to create feature request at YouTrack concerning your suggestion (https://youtrack.jetbrains.com/issues). Thank you!

0

Please sign in to leave a comment.