What is null in Kotlin?
In Kotlin, null represents the absence of a value. By default, Kotlin enforces strict null safety, meaning variables cannot hold a null value unless explicitly declared nullable. Nullable types are denoted by appending a question mark (?
) to the type declaration. For example, String?
represents a nullable String type.
Safe access operator (?.
)
To safely access nullable types, Kotlin provides the safe access operator (?.
). This operator allows you to perform operations on a nullable variable only if it is not null. If the variable is null, the expression returns null instead of throwing a null pointer exception.
fun printLength(str: String?) {
val length = str?.length
println("Length: $length")
}
In the above example, the safe access operator ?.
is used to access the length
property of the nullable str
variable.
Safe Calls in Chains
We can use the safe access operator (?.
) for chain calls.
class Person(val name: String?, val address: Address?)
class Address(val street: String, val city: String)
fun printStreet(person: Person?) {
val street = person?.address?.street
println("Street: $street")
}
If any step in the chain person?.address?.street
returns null, the entire expression evaluates to null, ensuring safe execution.
Elvis Operator (?:
)
Elvis operator (?:
), which allows you to provide a default value when encountering a null value.
fun printLength(str: String?) {
val length = str?.length ?: 1
println("Length: $length")
}
This example is a modified version of the previous example. If the argument str
is null
the value of the length
variable will be 1(The default value provided using the Elvis operator).
Smart Cast
Kotlin's type system includes smart casts, which enable the compiler to perform type checks and automatically cast variables to non-nullable types within a certain scope.
fun processString(str: String?) {
if (str is String) {
// Within this block, str is automatically cast to a non-nullable String
println(str.length)
}
}
Inside the if block scope compiler smart casts the str
variable to a non-nullable String
type, allowing direct access to its properties and methods.
The "!!" Operator
The !!
is used to assert that the value is non-null. If we use this operator and if the value is null, then NullPointerException
will be thrown.
fun processNonNullString(str: String?) {
val length = str!!.length
println("Length: $length")
}
In the above example if the str
is null, a NullPointerException
(NPE) will be thrown. Because of the chance of NPE, we should be careful while using non-null operator !!
.
Conclusion
In conclusion, Kotlin's null safety system provides developers with a powerful tool to eliminate null pointer exceptions and enhance the robustness and reliability of their code. By leveraging features such as safe access, the Elvis operator, and smart casts, developers can write safer and more maintainable code in Kotlin. Happy coding!
You can find more details from https://kotlinlang.org/docs/null-safety.html#nullable-types-and-non-null-types