Introduction
Null safety is a key feature in Kotlin designed to eliminate the danger of null references, commonly known as the "billion-dollar mistake". Kotlin provides several mechanisms to handle nullability in a safe and concise way. This chapter will cover the syntax and usage of nullable types, the safe call operator, the Elvis operator, and other null-safety features in Kotlin.
Nullable Types
In Kotlin, types are non-nullable by default. If you want a variable to hold a null value, you must explicitly declare it as nullable by appending a question mark (?
) to the type.
Syntax
var variable: Type? = null
Example
fun main() {
var name: String? = "John"
println(name)
name = null
println(name)
}
Explanation:
var name: String? = "John"
: Declares a nullable string variablename
.name = null
: Assigns a null value toname
.
Output:
John
null
Safe Call Operator (?.)
The safe call operator (?.
) allows you to access properties and methods of a nullable object without risking a null pointer exception. If the object is null, the expression returns null.
Example
fun main() {
var name: String? = "John"
println(name?.length) // Safe call
name = null
println(name?.length) // Safe call returns null
}
Explanation:
name?.length
: Safely accesses thelength
property ofname
. Ifname
is null, it returns null.
Output:
4
null
Elvis Operator (?:)
The Elvis operator (?:
) provides a default value if the expression to the left is null. It is a concise way to handle nullability and provide a fallback value.
Example
fun main() {
var name: String? = "John"
val length = name?.length ?: -1
println(length) // Prints 4
name = null
val length2 = name?.length ?: -1
println(length2) // Prints -1
}
Explanation:
val length = name?.length ?: -1
: Ifname
is null,length
is assigned-1
. Otherwise, it is assigned the length ofname
.
Output:
4
-1
Safe Cast Operator (as?)
The safe cast operator (as?
) attempts to cast a value to a specified type. If the cast is not possible, it returns null instead of throwing an exception.
Example
fun main() {
val obj: Any = "Kotlin"
val str: String? = obj as? String
println(str) // Prints "Kotlin"
val num: Int? = obj as? Int
println(num) // Prints "null"
}
Explanation:
val str: String? = obj as? String
: Safely castsobj
toString
. If the cast fails, it returns null.
Output:
Kotlin
null
Non-Null Assertion Operator (!!)
The non-null assertion operator (!!
) converts any value to a non-null type and throws a NullPointerException
if the value is null. Use this operator only when you are sure that the value is not null.
Example
fun main() {
var name: String? = "John"
println(name!!.length) // Prints 4
name = null
// println(name!!.length) // Throws NullPointerException
}
Explanation:
name!!.length
: Asserts thatname
is not null. Ifname
is null, it throws aNullPointerException
.
Output:
4
let Function
The let
function is an extension function that executes a block of code only if the object is not null. It can be used with the safe call operator.
Example
fun main() {
var name: String? = "John"
name?.let {
println("Name is not null: $it")
}
name = null
name?.let {
println("This will not be printed")
}
}
Explanation:
name?.let { ... }
: Executes the block only ifname
is not null.
Output:
Name is not null: John
Example Program with Null Safety
Here is an example program that demonstrates various aspects of null safety in Kotlin:
fun main() {
// Nullable types
var name: String? = "John"
println(name)
name = null
println(name)
// Safe call operator
name = "Kotlin"
println(name?.length)
name = null
println(name?.length)
// Elvis operator
val length = name?.length ?: -1
println(length)
// Safe cast operator
val obj: Any = "Kotlin"
val str: String? = obj as? String
println(str)
val num: Int? = obj as? Int
println(num)
// Non-null assertion operator
name = "John"
println(name!!.length)
// let function
name?.let {
println("Name is not null: $it")
}
name = null
name?.let {
println("This will not be printed")
}
}
Output:
John
null
6
null
-1
Kotlin
null
4
Name is not null: John
Conclusion
In this chapter, you learned about null safety in Kotlin, including how to use nullable types, the safe call operator, the Elvis operator, the safe cast operator, the non-null assertion operator, and the let
function. Null safety is a crucial feature in Kotlin that helps prevent null pointer exceptions and makes your code more robust and maintainable. Understanding and applying these null-safety mechanisms will significantly enhance the reliability of your Kotlin applications.