PrepZone Logo
PrepZone
Back to Java

Java · Easy

Why is string concatenation using + in a loop inefficient? How does the compiler optimize it?

JavaCore JavaStrings

Answer preview

Using + for string concatenation inside a loop is inefficient because each + creates a new String object — strings are immutable in Java.…