skip to content

What is `Unit` in Kotlin, how does it differ from Java's `void`, and where does it appear?

level: middleimportance: must knowfreq 60%

answer

  1. Unit = 'done, no useful value', a real singleton type
  2. void is a keyword; Unit is a type -> works in generics/lambdas
  3. side-effect lambdas type as () -> Unit
  4. statement-position if/loops/assignments => Unit
  5. Nothing = never returns (throw/TODO); has no instances

basics

~20 s

Unit is Kotlin's 'no meaningful result' type. A function that doesn't return anything useful returns Unit. Unlike Java's void keyword, Unit is a real type with a single value, so it can be used in generics and lambdas.

solid answer

~40 s

`Unit` is the type returned by functions that do something for their side effect and produce no meaningful value — the rough equivalent of Java's `void`, but `Unit` is a real singleton **type** with exactly one value, also written `Unit`. Because it is a genuine type, it can fill a generic type parameter (`List<Unit>`, `Function0<Unit>`) and serves as the return type of side-effecting lambdas, whereas Java's `void` cannot. Functions declaring no return type implicitly return `Unit`; you may write `: Unit` but it's idiomatic to omit it. A statement-position `if` with no `else`, or a loop, evaluates to `Unit`. In lambdas, the last expression's value becomes the result, so a `Unit`-returning functional type ignores it. `Unit` differs from `Nothing`, which is the type of expressions that never return (e.g. `throw`, `TODO()`).

code

kotlin · 4 lines
kotlin
fun save(x: Int) { /* side effect */ }   // : Unit implied
val handlers: List<() -> Unit> = listOf({ save(1) }, { save(2) })

fun crash(): Nothing = throw RuntimeException()  // Nothing, not Unit

go deeper

for a junior

Knows a function with no return value returns Unit and that it's like void.

for a middle

Explains Unit is a real singleton type usable in generics/lambdas, unlike the void keyword.

for a senior

Contrasts Unit with Nothing, knows statement-position constructs evaluate to Unit, and why () -> Unit is the side-effect lambda type.

for a principal

Can reason about Nothing as the bottom type, Unit's role in type inference, and how this shapes Kotlin's expression-oriented type system.

## What `Unit` is `Unit` is a type with a **single instance**, also called `Unit`. It represents "the function completed but has no meaningful value to give back." A function with no declared return type returns `Unit`: ```kotlin fun log(msg: String) { println(msg) } // returns Unit implicitly fun log2(msg: String): Unit { println(msg) } // explicit, but verbose/unidiomatic ``` ## `Unit` vs Java `void` - Java's `void` is a **keyword**, not a usable type — you cannot have `List<void>` or a `void` variable. - Kotlin's `Unit` is a real **type** (a singleton object), so it works everywhere a type is expected: generics, lambda return types, type parameters. ```kotlin val callbacks: List<() -> Unit> = listOf({ println("a") }) ``` This is why callbacks/side-effecting lambdas in Kotlin type as `() -> Unit`. ## Where `Unit` shows up - The return type of side-effecting functions. - The value of a **statement-position** construct: an `if` without `else`, `while`/`for` loops, and assignments all evaluate to `Unit`. - The implicit result of a lambda whose functional type is `... -> Unit` — the last expression's value is discarded. ## `Unit` vs `Nothing` `Nothing` is a separate type with **no instances**; it is the type of expressions that never produce a value because control flow leaves: `throw`, `TODO()`, `error(...)`, infinite loops. `Unit` means "completed, no value"; `Nothing` means "never completes normally". ```kotlin fun fail(): Nothing = throw IllegalStateException() val name = config.name ?: fail() // Nothing fits any type, so name is non-null String ``` ## Key APIs/keywords `Unit`, `Nothing`, implicit return type, `() -> Unit` functional type.

  • Can a lambda of type `() -> Unit` have a non-Unit last expression?
    Yes — the compiler coerces/ignores the last expression's value because the declared return type is Unit, so its value is simply discarded.
  • Why is `Nothing` a subtype of every type?
    Because an expression that never returns can be used anywhere; making Nothing a subtype of all types lets `val x: String = throw e` and `?: fail()` type-check.

void is a sign saying 'nothing here'; Unit is an actual empty box you can hand around and put in a list of boxes.

saying these in an interview costs you the question

  • Saying Unit and void are exactly the same
  • Claiming Unit has many instances or is null
  • Confusing Unit with Nothing
  • Thinking you must always write `: Unit` explicitly
  • Believing loops or assignments are expressions that return their operand

context