I'm learning Kotlin in Android, and I'm seeing some things return a type with a !
, for example, WifiInfo.getSSID() returns a string that is of type String!
. What does the ! do?
Bonus question, why can't I find this in the Kotlin documentation?
I'm learning Kotlin in Android, and I'm seeing some things return a type with a !
, for example, WifiInfo.getSSID() returns a string that is of type String!
. What does the ! do?
Bonus question, why can't I find this in the Kotlin documentation?
?
says "this can be null
".
!
says "we do not know if this can be null
or not, but we will assume that it cannot".
In Android and other Kotlin/JVM environments, you get !
from "platform types" — basically, types returned from libraries (e.g., the Android SDK) where the return value was not annotated with @Nullable
or @NotNull
.