y
05/24/2024, 8:26 PMStringBuilder
actually "efficient"? naively you're constructing a String by pushing a bunch of values which may reallocate partly through the operation, perhaps several times
there's nothing special going on here, is there?
in fact, I'd expect string formatting to be more amenable to optimizationdmays
05/24/2024, 9:13 PMy
05/24/2024, 9:17 PMSam
05/27/2024, 10:12 AMkotlin.text.StringBuilder
is currently implemented by calling straight through to java.lang.StringBuilder
. But it's also part of the standard library on other platforms, including JS and Native. The fact that it sometimes uses a Java class under the hood is an implementation detail which isn't necessarily immediately obvious. I would say that "Kotlin behaves like Java in this respect" is actually just a good way to answer this question, not a reason to dismiss the question as off-topic.Klitos Kyriacou
05/28/2024, 8:31 AMKlitos Kyriacou
05/28/2024, 8:42 AMStringBuilder().append(someInt).append(anotherInt)
in Kotlin/Native, it does StringBuilder().append(someInt.toString()).append(anotherInt.toString())
so you get intermediate object instances, whereas in the JVM implementation it renders the textual representations directly into the array owned by the StringBuilder.