Question
In Kotlin, what is the correct way to check whether a value is null?
For example, should I write:
if (a === null) {
// do something
}
or:
if (a == null) {
// do something
}
And for checking that a value is not null, should I use:
if (a !== null) {
// do something
}
or:
if (a != null) {
// do something
}
I want to understand which form is preferred in Kotlin and why.
Short Answer
By the end of this page, you will understand how Kotlin handles null checks, the difference between == and ===, and why a == null and a != null are the usual choices. You will also learn when referential equality with === matters and how null checks fit into everyday Kotlin code.
Concept
In Kotlin, null checking is closely tied to equality operators.
Kotlin has two main kinds of equality:
==→ structural equality===→ referential equality
Structural equality: ==
== checks whether two values are considered equal. In Kotlin, this is translated safely and works well with null.
For example:
a == null
means: "Is a equal to null?"
This is the standard and preferred way to check for null in Kotlin.
Likewise:
a != null
is the standard way to check that a value is not null.
Referential equality: ===
=== checks whether two references point to the in memory.
Mental Model
Think of Kotlin equality like comparing people in two different ways.
==asks: Do these two look the same or represent the same value?===asks: Are these literally the exact same person standing here?
Now think about null as "nobody is there."
If you want to check whether a spot is empty, you usually just ask:
a == null→ "Is nobody there?"
You do not need the stricter identity-style question for normal null checks.
So:
== null= the normal empty-check!= null= the normal not-empty-check==== special identity comparison, used for object reference questions, not everyday null checks
Syntax and Examples
The usual Kotlin syntax for null checking is:
if (value == null) {
// handle null
}
if (value != null) {
// safe to use value in this block
}
Example 1: Basic null check
fun printName(name: String?) {
if (name == null) {
println("No name provided")
} else {
println("Name: $name")
}
}
Here, name is nullable because its type is String?.
- If
nameisnull, the first branch runs. - Otherwise, Kotlin knows
nameis not null inside theelseblock.
Example 2: Not-null check
fun {
(name != ) {
println()
}
}
Step by Step Execution
Consider this example:
fun showLength(text: String?) {
if (text != null) {
println(text.length)
} else {
println("Text is null")
}
}
Now call it like this:
showLength("Kotlin")
showLength(null)
First call: showLength("Kotlin")
textreceives the value"Kotlin".- Kotlin evaluates
text != null. - Since
textis not null, the condition istrue. - The first block runs.
- Kotlin smart-casts
textfromString?toStringinside that block. text.lengthis printed, which is6.
Second call:
Real World Use Cases
Null checks are everywhere in Kotlin applications because many values may be optional or missing.
API responses
A server may return optional fields:
data class User(val nickname: String?)
fun printNickname(user: User) {
if (user.nickname != null) {
println(user.nickname)
} else {
println("No nickname")
}
}
Form input validation
A field may be blank or absent:
fun validateEmail(email: String?) {
if (email == null) {
println("Email is required")
}
}
Database values
Columns can contain null:
fun processMiddleName(middleName: String?) {
if (middleName != null) {
println(middleName.uppercase())
}
}
Real Codebase Usage
In real Kotlin projects, null checks are often combined with Kotlin features that reduce boilerplate.
Guard clauses
Developers often return early when a required value is missing:
fun sendEmail(address: String?) {
if (address == null) return
println("Sending email to $address")
}
Validation before processing
fun saveUsername(username: String?) {
if (username == null) {
throw IllegalArgumentException("Username cannot be null")
}
println("Saving $username")
}
Smart casts after != null
fun printUppercase(value: String?) {
if (value != null) {
println(value.uppercase())
}
}
Prefer Kotlin null-safety tools when possible
Common Mistakes
Mistake 1: Using === for everyday null checks
if (a === null) {
// works, but not idiomatic
}
This is valid, but it suggests referential comparison when a normal null check is all you need.
Prefer:
if (a == null) {
// idiomatic Kotlin
}
Mistake 2: Confusing == and ===
Beginners often think:
==is "double equals"===is "extra strict equals"
But in Kotlin, the difference is not about being stricter in general. It is about value equality vs reference identity.
val a = "hi"
val b = "hi"
println(a == b) // true: same value
println(a === b) // may be true or false depending on reference identity
Mistake 3: Forcing non-null with !!
Comparisons
| Operator | Meaning | Typical use | Recommended for null check? |
|---|---|---|---|
== | Structural equality | Compare values | Yes |
!= | Structural inequality | Check values are different | Yes |
=== | Referential equality | Check same object/reference | Usually no |
!== | Referential inequality | Check different references | Usually no |
== null vs === null
Cheat Sheet
Preferred null checks
value == null
value != null
Equality operators in Kotlin
== // structural equality
!= // structural inequality
=== // referential equality
!== // referential inequality
Rules of thumb
- Use
== nullto check for null - Use
!= nullto check for not null - Use
===only when you truly care about object identity - After
if (value != null), Kotlin can often smart-castvalue
Example
fun example(text: String?) {
if (text != null) {
println(text.length)
}
}
Related null-safety tools
text?.length // safe call
text ?:
text!!.length
FAQ
Should I use == null or === null in Kotlin?
Use == null in normal Kotlin code. It is the idiomatic and preferred way to check for null.
Is a === null wrong in Kotlin?
No, it is not wrong. It works, but it is not the usual style for null checks.
What is the difference between == and === in Kotlin?
== checks value equality. === checks whether two references point to the exact same object.
Should I use != null or !== null?
Use != null for standard not-null checks.
Why does Kotlin allow both forms for null?
Because null can still be compared using either equality model, but Kotlin style strongly favors == and != for normal null checking.
Does if (value != null) make the value non-null inside the block?
Mini Project
Description
Build a small Kotlin utility that prints information about an optional username. This project helps you practice idiomatic null checking with == null and != null, and shows how Kotlin smart casts work in real code.
Goal
Create a function that safely handles a nullable username and prints different messages depending on whether the value exists.
Requirements
- Create a function that accepts a nullable
String. - Print a fallback message when the value is
null. - Print the username and its length when the value is not
null. - Use
== nullor!= nullfor the checks. - Call the function with both a real string and
null.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Allow HTTP and HTTPS in Android 9 Pie with Network Security Configuration
Learn how Android 9 Pie handles cleartext HTTP traffic and how to allow HTTP and HTTPS safely using network security config.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.