โ† Back to list

๐Ÿš€ 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โ€ฆ

Yodgorbek Komilov ยท 2025-03-08 09:03 ยท 50 claps ยท 1.8 min read
#kotlin-day-5 #kotlin-null-safety #kotlin #kotlin-for-beginners
Open on Medium โ†—
Wiki topics: SAF ยท Safety & Alignment ๐Ÿ“ฑ ยท Mobile Development

๐Ÿš€ 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