0

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?

Khantahr
  • 8,156
  • 4
  • 37
  • 60

1 Answers1

2

? 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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491