there is something i don’t understand. is everythi...
# getting-started
l
there is something i don’t understand. is everything non-nullable by default (unless i use “?”) or is there something specific to write to ensure that something can’t be nulled ?
a
There is no default. Without
?
you explicitly make it non-nullable.
e
in Kotlin, a concrete type written as
String
is non-nullable,
String?
is nullable. types coming from Java have unknown nullability unless they have a recognized annotation, https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types
✔️ 1
if you have a generic type
<T>
in Kotlin, without further constraints, it does not have a specified nullability. you can use a constraint such as
<T : Any>
to ensure it is always a non-nullable type, or write
T?
or
T & Any
(the latter is new in Kotlin 1.7) to make specific use-sites nullable or non-nullable
✔️ 2
s
make sure to know about platform types to understand why extra attention is required when interacting with java code (nulls might leak into your non-nullable variables when interacting with Java). https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types
l
i’ve read the documentation but somehow, compared to other part of the documentation, this one isn’t very beginner friendly. i found a lot of explanation that use String as an exemple, but i wasn’t sure if String was a specific case or if everything was non-nullable unless specified otherwise.
(+ some exception with interop)
anyway, you answered my question. thank you