StringBuilder and Kotlin

Well, the difference between using explicit StringBuilder vs string interpolation or concatenation is that StringBuilder, being a Java class, is guarded with not-null assertions all over the place, so one can say that the compiler does a poor job handling explicit calls to StringBuilder. This is the only thing that makes concatenation/interpolation byte code better.

It is not fair to say that StringBuilder is less necessary in Kotlin: its main purpose is optimizing multiple concatenations, for example, when one conatenates strings in a loop. Without a StringBuilder such code has quadratic time complexity, which can kill your program’s performance on longer inputs.

1 Like