0
0
KotlinComparisonBeginner · 3 min read

Unit vs Void in Kotlin: Key Differences and Usage

In Kotlin, Unit is a type that represents the absence of a meaningful return value and is used as a return type for functions that do not return anything. Unlike Java's void, Unit is a real type with a single value, allowing it to be used as a generic type or expression result.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of Kotlin's Unit and Java's void types.

AspectKotlin UnitJava void
Type CategoryActual type with a single valueKeyword representing no return type
Return ValueReturns a singleton instance UnitNo value returned
Usage in GenericsCan be used as a generic type argumentCannot be used in generics
Function Return TypeExplicitly declared or implicit for no meaningful returnDeclared as void for no return
Expression UsageCan be used as an expression resultCannot be used as an expression
Interop with JavaMaps to void when calling Java codeN/A
⚖️

Key Differences

Unit in Kotlin is a real type that has exactly one value, also named Unit. This means functions returning Unit actually return this singleton value, allowing Unit to be used in places where a type is required, such as generics or expressions.

In contrast, Java's void is a keyword that indicates a function does not return any value. It is not a type and cannot be used as a value or in generics. This makes void more limited compared to Kotlin's Unit.

Because Unit is a proper type, Kotlin functions that return no meaningful value can still be treated uniformly with other functions that return values, improving consistency and flexibility in the language.

⚖️

Code Comparison

This Kotlin example shows a function returning Unit implicitly and explicitly.

kotlin
fun printMessage(): Unit {
    println("Hello from Unit function")
}

fun printMessageImplicit() {
    println("Hello from implicit Unit function")
}

fun main() {
    val result: Unit = printMessage()
    printMessageImplicit()
    println("Result is: $result")
}
Output
Hello from Unit function Hello from implicit Unit function Result is: kotlin.Unit
↔️

Java void Equivalent

This Java example shows a void function that does not return a value.

java
public class VoidExample {
    public static void printMessage() {
        System.out.println("Hello from void function");
    }

    public static void main(String[] args) {
        printMessage();
        // Cannot assign void to a variable
    }
}
Output
Hello from void function
🎯

When to Use Which

Choose Unit in Kotlin whenever you write functions that do not return meaningful values but you want to keep type consistency or use the function as an expression or generic argument. It is the idiomatic way in Kotlin.

Use void only when interoperating with Java code or when writing Java itself, as Kotlin maps Unit to void in Java bytecode for compatibility.

Key Takeaways

Unit is a real type in Kotlin with a single value, unlike Java's void keyword.
Kotlin functions returning no meaningful value return Unit, enabling use in generics and expressions.
void cannot be used as a value or generic type and is limited to Java methods with no return.
Use Unit in Kotlin for idiomatic, consistent code and void only for Java interop.
Kotlin's Unit improves flexibility and uniformity in function return types.