How does string interpolation work in Kotlin?

前端 未结 4 1438
囚心锁ツ
囚心锁ツ 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 11:13

    Regarding your 2nd question: If you need caching for fullName, you may and should do it explicitly:

    class Client {
        val firstName: String
        val lastName: String
        val fullName = "$firstName $lastName"
    }
    

    This code is equivalent to your snipped except that the underlying getter getFullName() now uses a final private field with the result of concatenation.

提交回复
热议问题