Efficient String Construction
Repeated concatenation of immutable strings inside a loop can become O(n²) because each concatenation may allocate a new string and copy the existing contents. As the accumulated string grows, the amount of copying grows as well.
Immutable strings cannot be modified in place.
Repeated concatenation may create many temporary objects.
The cumulative copying can become quadratic for a growing result.
StringBuilder maintains a mutable buffer and grows its capacity as needed.
With suitable capacity management, StringBuilder typically provides near-linear total construction time.