Use StringBuffer to concatenate strings

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

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();
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes