Strings In Depth11 / 18
Up next

StringBuilder

class Main {
    public static void main(String[] args) {
        // appending to a String in a loop reallocates each time
        var sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            sb.append(i);
        }
        System.out.println(sb.toString());
    }
}
What you're solving

build text without a new object each step