String字符串连loop里不要用“+”

最近项目优化,有如下一段代码:

String readString = "";
            String currentLine;
 while ((currentLine = in.readLine()) != null) {
                readString += currentLine;
            }

lint检测粗warning:
Reports String concatenation in loops. As every String concatenation copies the whole String, usually it is preferable to replace it with explicit calls to StringBuilder.append() or StringBuffer.append().
stackoverflow中有问题解答:
https://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java

重点一句是:
but at what point do you switch from concat to builder?
回答:
At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.

你可能感兴趣的:(String字符串连loop里不要用“+”)