What is the Unit type in Kotlin, and how does it differ from Java's void? When can you omit it from a function signature?
answer
- Unit = real type, one value, named Unit
- void is a keyword, Unit is a type
- Omit `: Unit` — it is inferred
- Unit fits generics: Function0<Unit>
- Lambda last expression ignored (Unit-coercion)
basics
~20 sUnit is Kotlin's type for functions that do useful work but return no meaningful value. It is like Java's void, but it is a real type with one value. You can leave it out of the signature.
solid answer
~40 sUnit is a real singleton type with exactly one value, also written Unit. A function with no declared return type returns Unit implicitly, so `fun log(s: String)` and `fun log(s: String): Unit { ... }` are identical. Unlike Java's `void` (which is not a real type and cannot be used as a generic argument), Unit is a normal type, so it can fill type parameters: `Function0<Unit>`, `List<Unit>`, or a `suspend () -> Unit` lambda. Because Unit is a value, the compiler implicitly returns it; you rarely write `return Unit` explicitly. In lambdas, the last expression's value is ignored when the expected return type is Unit (Unit-coercion), so a lambda body can end in any expression.
code
kotlin · 5 linesfun save(item: String) { /* returns Unit */ }
// Unit as a generic argument — impossible with Java void
val tasks: List<() -> Unit> = listOf({ println("a") }, { println("b") })
val result: Unit = save("x") // valid: Unit is a valuego deeper
Knows Unit means 'no meaningful return' and that it is omittable, roughly like void.
Explains Unit is a real singleton type with one value and why that beats void for generics/lambdas.
Discusses Unit-coercion in lambdas and JVM-level compilation to void for interop.
Frames Unit as part of a uniform type system (everything is a value/type), contrasting language designs that special-case void.
## What Unit is `Unit` is an ordinary Kotlin type that has **exactly one instance**, also named `Unit` (it is declared `object Unit`). A function that performs an action but has nothing meaningful to give back returns `Unit`. ```kotlin fun greet(name: String): Unit { println("Hi, $name") // compiler inserts `return Unit` } // identical — the `: Unit` is omittable fun greet2(name: String) { println("Hi, $name") } ``` When you do not write a return type, the compiler infers `Unit`. So omitting it is the idiomatic style. ## Unit vs Java's `void` - Java's `void` is **not a type** — it is a keyword marking "no return." You cannot write `List<void>` or pass `void` as a generic argument. - `Unit` **is a type with a value**, so it works everywhere a type is expected: `Function0<Unit>`, `Callable<Unit>`, `suspend () -> Unit`. - This matters for generics and functional code. A higher-order function `fun <T> run(block: () -> T): T` can be called with a block returning `Unit`; with `void` that would be impossible. At the JVM bytecode level a Unit-returning Kotlin function compiles to a `void` method for interop, but in the Kotlin type system it is `Unit`. ## Unit-coercion in lambdas If the expected type of a lambda is `() -> Unit`, the value of its last expression is **discarded**, so the body may end with any expression: ```kotlin val action: () -> Unit = { 42 } // 42 is ignored; lambda returns Unit ``` ## When you can omit it - Always in a function declaration: leave off `: Unit`. - You almost never write `return Unit`; a bare `return` (or falling off the end) suffices. ## Key terms - **Singleton / object**: a type with a single shared instance. - **Type parameter / generic argument**: the `T` in `List<T>`; Unit can be one, `void` cannot.
- Why can't you use Java's void as a generic type argument but you can use Unit?void is a keyword, not a type, so the type system has no object to substitute for a type parameter. Unit is a real type with a value, so it substitutes normally.
- Does a Unit-returning Kotlin function compile to a Unit or void method on the JVM?To a `void` JVM method for clean Java interop; Unit exists only in the Kotlin type system.
Unit is like signing a delivery receipt: the action happened, and the single signature is the only 'value' that comes back.
saying these in an interview costs you the question
- Saying Unit and void are exactly the same with no nuance
- Claiming you must write `return Unit` explicitly
- Thinking Unit can hold multiple distinct values
- Believing Unit cannot be used as a generic argument
- Confusing Unit (no value) with Nothing (no return)