Java

Java

Made by DeepSource

String concatenation using + within loops is costly and should be replaced by a StringBuffer/StringBuilder JAVA-P1006

Performance
Major

The method seems to be building a String using concatenation in a loop. In each iteration, the String is converted to a StringBuffer/StringBuilder, appended to, and converted back to a String.

This can lead to a cost of `O(n^2)`` where n is the number of iterations, as the growing string is recopied in each iteration. This issue is detected only on Java versions 8 and below.

Bad Practice

String s = "";
for (int i = 0; i < field.length; ++i) {
    s = s + field[i];
}

Recommended

Create a StringBuffer outside the loop, then update it from within.

StringBuffer buf = new StringBuffer();
for (int i = 0; i < field.length; ++i) {
    buf.append(field[i]);
}
String s = buf.toString();