๐ Kotlin Day 5: Null Safety in Kotlin ๐
Null safety is one of Kotlinโs most powerful features, designed to eliminate the notorious NullPointerException (NPE). Unlike Java, whereโฆ
๐ Kotlin Day 5: Null Safety in Kotlin ๐
Null safety is one of Kotlinโs most powerful features, designed to eliminate the notorious NullPointerException (NPE). Unlike Java, where any object reference can be null by default, Kotlin enforces strict null safety rules, making code safer and more predictable.
โ How Null Safety Works in Kotlin
By default, variables in Kotlin cannot be null. If you want a variable to hold a null value, you must explicitly declare it as nullable using ?.
๐ Key Features of Null Safety
1๏ธโฃ Nullable Types (?)
If a variable needs to hold a null value, you must explicitly declare it with ?:
var name: String? = "Kotlin"
println(name) // Output: Kotlin
name = null // โ
Allowed because it's a nullable type
println(name) // Output: null
However, if name were declared as var name: String, assigning null would result in a compilation error.
2๏ธโฃ Safe Call Operator (?.) โ Avoids direct null access
The safe call operator ?. ensures that a method is only called if the variable is not null. If the variable is null, the entire expression is evaluated as null.
var name: String? = "Kotlin"
println(name?.length) // Output: 6 (Length of "Kotlin")
name = null
println(name?.length) // Output: null (Does not crash!)
Without ?., trying to call name.length when name is null would cause a NullPointerException.
3๏ธโฃ Elvis Operator (?:) โ Provides a default value
If a nullable variable is null, the Elvis operator (?:) provides a default value:
val name: String? = null
val length = name?.length ?: 0 // If name is null, use default value 0
println("Length: $length") // Output: Length: 0
This is useful when you want to avoid returning null and provide a meaningful fallback.
4๏ธโฃ Safe Casting (as?) โ Prevents ClassCastException
Instead of throwing an exception, as? returns null if the cast fails:
val obj: Any = "Hello"
val str: String? = obj as? String // Safe cast, returns null if not a String
println(str) // Output: Hello
val number: Int? = obj as? Int // Trying to cast String to Int
println(number) // Output: null (instead of throwing an exception)
If you used obj as Int, it would throw a ClassCastException.
5๏ธโฃ Not-Null Assertion (!!) โ Forces a value (โ ๏ธ Use with caution)
The !! operator tells Kotlin: "I am sure this value is not null". If the variable is actually null, it will throw an NPE:
var name: String? = "Kotlin"
println(name!!.length) // Output: 6
name = null
// println(name!!.length) // โ Throws NullPointerException!
โ ๏ธ Use !! only when you are absolutely sure that the variable is never null! Otherwise, your app may crash.
๐ฏ Conclusion
Kotlinโs null safety system eliminates many runtime crashes caused by NullPointerException in Java. By using:
โ
Safe calls (?.) to prevent errors
โ
The Elvis operator (?:) to provide default values
โ
Safe casting (as?) to avoid class cast exceptions
โ
Avoiding !! unless necessary
๋ฉํ๋ฐ์ดํฐ
- post_id
- 7cd2d1f8631c
- slug
- kotlin-day-5-null-safety-in-kotlin-7cd2d1f8631c
- url
- https://medium.com/@YodgorbekKomilo/kotlin-day-5-null-safety-in-kotlin-7cd2d1f8631c
- canonical_url
- https://medium.com/@YodgorbekKomilo/kotlin-day-5-null-safety-in-kotlin-7cd2d1f8631c
- author_url
- https://medium.com/@YodgorbekKomilo
- status
- ok
- fetched_at
- 2026-08-17 06:51:02