Java Tips

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

Java Tips
Java code TIPS
Play Framework2.5 (Java) Tips
Java
Java tips --Spring execution Summary
[Java] Tips for writing source
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
java beginner 4
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
Java High Level REST Client Tips
[Java] Polymorphism
Studying Java # 0
Java review
java framework
[Java] Inheritance
FastScanner Java
java beginner 3
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics
Java methods
Java inheritance
[Java] enum
[Java] FizzBuzzCounter
Studying Java ―― 8
Java array