Kotlin 编译器是否翻译 "Hello, $name!"
使用类似的东西
java.lang.String.format("Hello, %s!", name)
还是有其他机制?
如果我有这样的类(class),例如:
class Client {
val firstName: String
val lastName: String
val fullName: String
get() = "$firstName $lastName"
}
这个 getter 会返回一个缓存的字符串还是会尝试构建一个新的字符串?我应该改用lazyOf 委托(delegate)吗?
我知道除非有数百万次对 fullName
的调用,否则不会出现性能问题,但除了如何使用它之外,我还没有找到有关此功能的文档。
最佳答案
Kotlin 编译器将此代码翻译为:
new StringBuilder().append("Hello, ").append(name).append("!").toString()
没有执行缓存:每次评估包含字符串模板的表达式时,将再次构建结果字符串。
https://stackoverflow.com/questions/37442198/
相关文章:
android - 警告 "Kotlin plugin version is not the sam
variables - 在 Kotlin 中一次定义多个变量(例如 Java : String x,
gradle - 如何使用 kotlinscript DSL (build.gradle.kts)
constructor - Kotlin 数据类可以有多个构造函数吗?
android - RealmObject 的 Kotlin 数据类
kotlin - 为什么 UInt 没有 toDouble()?
spring - Kotlin 和 Spring Boot @ConfigurationProper