String -Performance is improved by using StringBuilder for string operations. → In particular, do not combine strings with + in a loop
bad.java
    static String concat(String[] array) {
        String result = "";
        for (String s : array) {
            result += s;  // 
        }
        return result;
    }
↓
better.java
    static String concat(String[] array) {
        StringBuiler result = new StringBuiler();
        for (String s : array) {
            result.append(s);
        }
        return result.toString();
    }
StringBuiler_equals.java
        StringBuilder sb1 = new StringBuilder("ABC");
        StringBuilder sb2 = new StringBuilder("ABC");
        if(sb1 == sb2){
            System.out.println("sb1 == sb2" + " is OK.");
        }else{
            System.out.println("sb1 == sb2" + " is NG.");//FALSE
        }
        if(sb1.equals(sb2)){
            System.out.println("sb1 equals sb2" + " is OK.");
        }else{
            System.out.println("sb1 equals sb2" + " is NG.");//FALSE
        }
        if(sb1.toString().contentEquals(sb2)){
            System.out.println("sb1 contentEquals sb2" + " is OK.");//TRUE
        }else{
            System.out.println("sb1 contentEquals sb2" + " is NG.");
        }   
        Recommended Posts