Use StringBuffer to concatenate strings
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、
お気軽にご連絡ください。
ID: java-best-practices/use-stringbuffer
Language: Java
Severity: Warning
Category: Performance
Description
Use StringBuffer
to create and concatenate long strings. Concatenating strings multiple times slows your program execution.
Non-Compliant Code Examples
class Main {
public static void main(String[] args) {
String result = "";
for (Person p: persons) {
foo();
result += p.getName() + ",";
bar();
}
for(int i = 0 ; i < persons.size() ; i++) {
foo();
result += persons.get(i).getName() + ",";
bar();
}
return result;
}
}
Compliant Code Examples
class Main {
public static void main(String[] args) {
StringBuilder result = new StringBuilder();
for (Person p: persons) {
result.append(p.getName() + ",");
}
return result.toString();
}
}
Seamless integrations. Try Datadog Code Analysis