
One of Kotlinโs biggest advantages over Java is built-in null safety.
If you've written Android apps in Java before, you probably remember how often apps crashed because of a NullPointerException.
Kotlin tries to solve this problem by making nullability part of the type system.
This means the compiler can help prevent many null-related bugs before your code even runs.
In this guide weโll cover the most important Kotlin null safety tools:
Nullable types (?)
Safe call operator (?.)
Not-null assertion (!!)
let with null checks
Best practices for Android developers
๐ Korean Tip
์ฝํ๋ฆฐ์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ค ํ๋๊ฐ Null Safety ์
๋๋ค.
Java์์ ์์ฃผ ๋ฐ์ํ๋ NullPointerException ๋ฌธ์ ๋ฅผ ์ปดํ์ผ ๋จ๊ณ์์ ์ค์ฌ์ฃผ๋ ๊ธฐ๋ฅ์
๋๋ค.
In Kotlin, variables are non-null by default.
This means the compiler will not allow you to assign null unless you explicitly say the variable can be nullable.
Example:
val name: String = "Kotlin"
This variable cannot be null.
If you want a variable that can hold null, you must add ?.
val name: String? = null
๐ Korean Tip
?๊ฐ ๋ถ์ผ๋ฉด null์ด ๋ค์ด๊ฐ ์ ์๋ ๋ณ์๊ฐ ๋ฉ๋๋ค.
The safe call operator allows you to safely access properties of nullable objects.
val name: String? = "Android"
println(name?.length)
If name is null, the expression simply returns null instead of crashing.
๐ Korean Tip
?. ์ฐ์ฐ์๋ null์ด๋ฉด ์คํํ์ง ์๊ณ null์ ๋ฐํํฉ๋๋ค.
Equivalent Java-style code would look like this:
if (name != null) {
println(name.length)
}The Kotlin version is much cleaner.
One of the most common patterns in Kotlin is combining safe calls with let.
val name: String? = "Kotlin"
name?.let {
println("Name is $it")
}This block runs only if the value is not null.
๐ Korean Tip
?.let { } ํจํด์ ์ฝํ๋ฆฐ์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ null ์ฒดํฌ ๋ฐฉ์์
๋๋ค.
Sometimes developers use !! to force a nullable value to be treated as non-null.
Example:
val name: String? = "Kotlin"
println(name!!.length)If name happens to be null, the app will crash.
๐ Korean Tip
!!๋ null์ด ์๋๋ผ๊ณ ๊ฐ์ ๋ก ๋จ์ ํ๋ ์ฐ์ฐ์์
๋๋ค.
ํ์ง๋ง null์ด๋ฉด ์ฑ์ด ๋ฐ๋ก crashํฉ๋๋ค.
Avoid using !! whenever possible.
Most experienced Kotlin developers consider frequent use of !! a code smell.
The Elvis operator provides a default value if something is null.
Example:
val name: String? = null
val result = name ?: "Guest"
println(result)Output
Guest
๐ Korean Tip
?: ๋ null์ผ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋ ์ฐ์ฐ์์
๋๋ค.
Here are some patterns you'll see often in Kotlin code.
user?.email
user?.let {
sendEmail(it)
}val name = user?.name ?: "Guest"
๐ Korean Tip
์ด ์ธ ๊ฐ์ง ํจํด์ Android Kotlin ์ฝ๋์์ ๊ฑฐ์ ๋งค์ผ ์ฌ์ฉํ๋ ํจํด์
๋๋ค.
This defeats the purpose of Kotlin null safety.
Example:
val name: String? = null
println(name!!.length)This will crash.ย
Sometimes developers forget to mark variables nullable.
Example:
val name: String = null
This will not compile.
user?.email
user?.let {
sendEmail(it)
}
val username = user?.name ?: "Guest"
๐ Korean Tip
Kotlin Null Safety์ ํต์ฌ์
?.
let
?:
์ด ์ธ ๊ฐ์ง๋ฅผ ์ ํ์ฉํ๋ ๊ฒ์ ๋๋ค.
| Feature | Example | Purpose |
|---|---|---|
| Nullable type | String? |
allow null |
| Safe call | ?. |
avoid crash |
| let | ?.let {} |
null check |
| Elvis operator | ?: |
default value |
| Not-null assertion | !! |
force non-null |
Because NullPointerException is one of the most common causes of app crashes.
Kotlinโs type system helps prevent these errors.
!!?Only when you are absolutely sure the value cannot be null.
Even then, many developers prefer safer alternatives.
This one:
value?.let { }
"If you would thoroughly know anything, teach it to other."
- Tryon Edwards -