String Builders Over Concatenation

When performing String concatenation in Java, it is very easy to fall into the trap of utilising the + operator to combing multiple strings together. However, this approach has major performance ramifications if performed a large number of times. Default String concatenation requires both strings contents to be copied, resulting in quadratic runtime (O(n2)).
Strings in Java are immutable - meaning that operations on a String never change the value of the String. Each time String A is concatenated with String B; memory is allocated to allow space for the combined length of the 2 new strings. Then the original reference to String A is replaced with its new concatenated form.
StringBuilder in contrast uses an internal array to copy over the characters, increasing the size of the array if needed when adding new String's.
public class TestStringConcatenation {
    public String usingStringConcatenation(String stringToAppend) {
        String startingString = "";
        
        for (int i = 0; i < 10_000; i++) {
            startingString += stringToAppend;
        }
        return startingString;
    }
    
    public String usingStringBuilder(String stringToAppend) {
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < 10_000; i++) {
            sb.append(stringToAppend);
        }
        return sb.toString();
    }
}
This can result in massive performance improvements when concatenations are ran numerous times. To improve performance further - if you know the size of the final string ahead of time, pre-allocate the StringBuilder's size.
StringBuilder sb = new StringBuilder(INITIAL_SIZE);
Minor caveat - if performing concatenation in a multi-threaded environment - then StringBuffer can be used - as it provides synchronisation for threads - however it is also slower than StringBuilder for this reason.

performance
strings