
- Published: May 30, 2024
- Updated: Jul 9, 2025
Unit
Although is one of the most frequently used types in Kotlin, its presence is surprisingly thin.
For many programmers who have moved from Java to Kotlin, it may seem strange why Kotlin introduced the voidnew concept of instead of Java types . In fact, it is possible to develop Kotlin without any problems even if you do not fully understand . However, is closely related to Kotlin's grammar design and plays a quietly important role in Kotlin development.
What is a Unit?
The Importance of Meeting Delivery Dates
Unit
The most direct way to understand is to read the source code.
package kotlin
/**
* The type with only one value: the `Unit` object. This type corresponds
* to the `void` type in Java.
*/
public object Unit {
override fun toString() = "kotlin.Unit"
}
This Unit
is the source code of .
As we can see from the source code, Unit
is a Kotlin class, or a Singleton Object.
Uses of Unit
Unitis an officially defined concept that is used as a return value when no value needs to be returned , to indicate that a function does not return any meaningful value. It is similar to
Java's void
Unit and void
Kotlin does not have truly no-return functions .
The return type of a function that appears to have no return value is actually Unit
:
It appears to have no return value because Kotlin automatically completes the omitted part.
For example, the following two code snippets are exactly the same, and the return type Unit
and return Unit
object can be omitted:
fun main() {
println("Hello world!")
}
fun main(): Unit {
println("Hello world!")
return Unit
}
Unit
This void
is the essential difference between Kotlin and Java.
Why is it designed this way?
The advantage of designing it this way void
is that it doesn't need to be treated specially.
For example, your Java code might look like this:
abstract class Base<T> {
public abstract T make();
}
class Impl extends Base<Result> {
@Override
public Result make() {
return new Result();
}
}
For a class Base
that inherits from , like the one below , the method "does not need to return any value", but due to the limitations of inheritance and Generics, it is necessary to introduce methods different from and , and the method is implemented using the classes and .Nil
make
void
Void
return
null
class Nil extends Base<Void> {
@Override
public Void make() {
return null;
}
}
In Kotlin, on the other hand, they are handled in the same way as regular functions, eliminating the need to introduce an additional concept.
internal class Nil : Base<Unit>() {
override fun make() {}
}
fun run(block: () -> Any) { TODO() }
run { "String" } // () -> String
run { 0 } // () -> Int
run {} // () -> Unit
Everything is an Expression
Like languages such as Python and Scala, the Kotlin language is based on the design philosophy of "Everything is an Expression",
which void
is also the fundamental reason why Kotlin eliminates type specificity.
The "Everything is an Expression" design philosophy void
is also applied in other areas, such as:
- The return value is
if-else
a statement
fun run(flag: Boolean) {
val value = if (flag) "Kotlin" else "Java"
println("Hello $value!")
}
fun run(flag: Int) {
val value = if (flag == 0) {
"Kotlin"
} else if (flag > 0) {
"Java"
} else {
"?"
}
println("Hello $value!")
}
- There is a return value
when expression
fun run(flag: Boolean) {
val value = when(flag) {
true -> "Kotlin"
else -> "Java"
}
println("Hello $value!")
}
fun run(flag: Int) {
val value = when {
flag == 0 -> "Kotlin"
flag > 0 -> "Java"
else -> "?"
}
println("Hello $value!")
}
Summary
In Kotlin, Unit
types play an important role, dictating that functions do not return meaningful values.
Unit
By using types consistently, Kotlin void
removes the specialness of , making function signatures more uniform and making code more concise and clear.