Convert StringBuilder to String concatenation suggestion

已回答

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?

4

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

0
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

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";
3

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

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.

2

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

3

Hello Chris,

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

 

1

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.

4

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!

2

There is a bug in intellij. Performance is always better with StringBuilder not sure why it suggests to use String over StringBuilder. Here is a small example:

2

Shivam1996anand, I agree with your test, though it's missing one more operation. You should also add the “.toString()” on line 17 to make it resulting the same.

But overall timing isn't gonna much diff from what your test have.

2

请先登录再写评论。