在 Kotlin 中,一个感叹号是什么意思?我见过几次,尤其是在使用 Java API 时。但我在文档和 StackOverflow 上都找不到它。
最佳答案
他们被称为 platform types他们的意思是 Kotlin 不知道该值是否可以为 null
并且由您决定它是否可以为空。
In a nutshell, the problem is that any reference coming from Java may be null, and Kotlin, being null-safe by design, forced the user to null-check every Java value, or use safe calls (
?.
) or not-null assertions (!!
). Those being very handy features in the pure Kotlin world, tend to turn into a disaster when you have to use them too often in the Kotlin/Java setting.This is why we took a radical approach and made Kotlin’s type system more relaxed when it comes to Java interop: now references coming from Java have specially marked types -- Kotlin Blog
https://stackoverflow.com/questions/43826699/