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?
请先登录再写评论。
Could you please share a sample .java file to reproduce?
In this basic case the efficiency would be the same. StringBuilder makes sense in a loop.
The compiled code will look like this:
Which the compiler will turn into:
Which, semantically, is no different to the original code.
True, but
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.
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
Hello Chris,
Could you please provide code example where IDE suggests to convert StringBuilder to concatenation and it decreases performance?
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.
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!
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:
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.