intermediateString & StringBuilder
Why can naive string concatenation in a loop hurt performance, and did Java 9 change this?
Strings are immutable, so `result = result + i` in a loop creates a brand-new String object every iteration — use StringBuilder instead. Java 9+ does optimize a single-expression concatenation like `"Hello " + name` via invokedynamic to be nearly as fast as StringBuilder, but this doesn't help multi-statement loops.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response