Question
Kotlin Void vs Unit vs Nothing: Differences, Uses, and Examples
Question
Kotlin has three types that appear similar at first glance:
VoidUnitNothing
They can seem comparable to JavaScript concepts such as null, undefined, and void 0, which raises the question of whether they overlap unnecessarily.
What is each of these Kotlin types used for, and how do they differ in meaning and behavior?
Short Answer
By the end of this page, you will understand what Void, Unit, and Nothing mean in Kotlin, when each one appears, and why they are not interchangeable. You will also learn how these types are used in function return types, Java interoperability, and expressions that never complete normally.
Concept
Kotlin uses Unit, Nothing, and sometimes Void for different purposes, even though they can look related.
Unit
Unit is Kotlin's way of saying:
"This function finishes normally, but it does not return any useful value."
It is similar to Java's void, but with one important difference: Unit is a real type in Kotlin. It has exactly one value: Unit.
fun printMessage(): Unit {
println("Hello")
}
In most Kotlin code, you omit it:
fun printMessage() {
println("Hello")
}
Kotlin still treats that function as returning Unit.
Mental Model
Think of a function like a delivery trip.
Unitmeans: the driver came back, but there was no package to deliver.Nothingmeans: the driver never comes back at all.Voidmeans: you are working with another company's paperwork system (Java), and they use a special form namedVoid.
So even though all three may look like "no value," they describe very different situations:
- one finishes normally
- one never finishes normally
- one mostly exists to match Java APIs
Syntax and Examples
Core syntax
Unit
fun log(message: String): Unit {
println(message)
}
Usually written without the explicit return type:
fun log(message: String) {
println(message)
}
Nothing
fun crash(): Nothing {
throw RuntimeException("Something went wrong")
}
Void
fun javaStyle(): Void? {
return null
}
This is uncommon in Kotlin and usually only appears when matching Java APIs.
Step by Step Execution
Consider this example:
fun getUsername(input: String?): String {
return input ?: throw IllegalArgumentException("Username cannot be null")
}
Now trace it.
Case 1: input is not null
getUsername("alice")
Step by step:
- The function receives
"alice". - Kotlin checks
input ?: .... - Since
inputis notnull, the left side is used. - The function returns
"alice". - The function completes normally.
Case 2: input is null
getUsername(null)
Step by step:
- The function receives
null. - Kotlin checks
input ?: .... - Since is , Kotlin evaluates the right side.
Real World Use Cases
Where Unit is used
Unit appears in functions that perform actions:
- logging
- printing output
- saving data
- sending analytics events
- updating UI state
- writing files
Example:
fun saveUser(user: User) {
repository.save(user)
}
The function does something useful, but there is no meaningful value to return.
Where Nothing is used
Nothing is common in code paths that must stop execution:
- validation failures
- impossible states
- helper functions that always throw
- program termination wrappers
Example:
fun requireAdmin(isAdmin: Boolean): Nothing {
throw IllegalAccessException("Admin access required")
}
A more realistic pattern is a helper used conditionally:
Real Codebase Usage
In real Kotlin projects, developers usually follow these patterns.
Unit for side-effect functions
Most application code uses Unit implicitly:
fun updateProfile(profile: Profile) {
validate(profile)
api.save(profile)
}
This is the normal choice for command-style functions.
Nothing for guard clauses and fail-fast code
A common pattern is writing helper functions that stop execution:
fun failValidation(message: String): Nothing {
throw IllegalArgumentException(message)
}
Then using them inside expressions:
val email = inputEmail ?: failValidation("Email is required")
This is especially useful for:
- validation
- null checking
- impossible branches
- exhaustive
whenhandling
Common Mistakes
1. Using Void instead of Unit in Kotlin code
Broken style:
fun logMessage(): Void? {
println("Hello")
return null
}
Better:
fun logMessage() {
println("Hello")
}
Why this is a mistake:
Voidis not idiomatic Kotlin- it often forces
null - it makes APIs less natural
2. Thinking Unit means null
Unit is a real value, not the same as null.
val result: Unit =
Comparisons
| Concept | What it means | Can it have a value? | Normal Kotlin use | Typical example |
|---|---|---|---|---|
Unit | Function completes normally with no meaningful result | Yes, exactly one value: Unit | Very common | fun log() { ... } |
Nothing | Function never completes normally | No | Used for throw, infinite loops, fail-fast helpers | fun fail(): Nothing { throw ... } |
Void | Java interop type (java.lang.Void) | Practically used as when nullable |
Cheat Sheet
Quick reference
Unit
- Means: no meaningful return value
- Function completes normally
- Kotlin's usual replacement for Java
void - Has one value:
Unit - Usually omitted in function declarations
fun printName() {
println("Sam")
}
Nothing
- Means: no value can ever be returned
- Function never completes normally
- Used for
throw, fatal errors, infinite loops - Has no values
fun fail(): Nothing {
throw IllegalStateException("Failed")
}
Void
- Means: Java interop type
java.lang.Void - Rare in Kotlin-first code
- Mostly used in Java generic APIs
- Often appears as with
FAQ
What is the difference between Unit and Nothing in Kotlin?
Unit means a function finishes normally but has no meaningful result. Nothing means a function never finishes normally, such as one that always throws an exception.
Is Kotlin Unit the same as Java void?
They are similar in purpose, but not identical. Unit is a real type with one value, while Java void is not a normal type you can use as a value.
When should I use Void in Kotlin?
Usually only when working with Java APIs that explicitly require Void, especially generic types like Callable<Void>.
Why does throw work inside expressions in Kotlin?
Because throw has type Nothing. Kotlin knows that branch never returns, so it can fit into expressions expecting another type.
Can a function returning Nothing return null?
Mini Project
Description
Build a small Kotlin validation utility that demonstrates all three concepts: a normal action function using Unit, a fail-fast helper using Nothing, and a Java interop example using Void. This project mirrors real application code where you validate input, log progress, and sometimes connect to Java libraries.
Goal
Create a small program that validates a username, logs a success message, and includes a Java Callable<Void> task.
Requirements
- Create a function that prints a log message and returns normally.
- Create a function that always throws an exception for invalid input.
- Create a function that validates a nullable username using the Elvis operator.
- Add a Java
Callable<Void>example that performs an action and returnsnull.
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.