How does string interpolation work in Kotlin?

前端 未结 4 1440
囚心锁ツ
囚心锁ツ 2020-12-25 10:17

Does the Kotlin compiler translate \"Hello, $name!\" using something like

java.lang.String.format(\"Hello, %s!\", name)

or is

4条回答
  •  一生所求
    2020-12-25 10:56

    The Kotlin compiler translates this code to:

    new StringBuilder().append("Hello, ").append(name).append("!").toString()
    

    There is no caching performed: every time you evaluate an expression containing a string template, the resulting string will be built again.

提交回复
热议问题