skip to content

Unit — the No-Value Type

Unit is a real type with exactly one value, returned by functions that exist for their side effects. Interviewers ask how it differs from Java's void and from Nothing, since the three are easy to conflate and only one of them means 'never returns'.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

What is kotlin.Unit, and what does a Kotlin function return when you don't declare a return type?

level: juniorimportance: must knowfreq 70%

answer

  1. One type, one value: the Unit singleton
  2. No return type => returns Unit
  3. Both `: Unit` and `return Unit` are implicit
  4. Unit is a real object, not a keyword
  5. Used by side-effecting functions

basics

~10 s

Unit is Kotlin's type for functions that do work but give back no useful value. If you don't write a return type, the function returns Unit automatically.

solid answer

~30 s

kotlin.Unit is a built-in type with exactly one value, the object Unit. Functions whose only purpose is a side effect (printing, mutating state) return Unit. The return type is optional: `fun log(msg: String) {}` is identical to `fun log(msg: String): Unit { return Unit }`. The Unit value and the `return` are both inserted implicitly by the compiler, so you never write them. It is Kotlin's analogue of Java's `void`, but unlike `void`, Unit is a real type you can name, store in a variable, and use as a generic type argument such as `Function0<Unit>` or `LiveData<Unit>`.

go deeper

for a junior

Knows Unit = the 'no useful value' return type and that omitting the return type implies it.

for a middle

Explains that Unit is a singleton object with one value and that both the type and the return are implicit.

for a senior

Connects Unit being a real type to its use in generic positions like () -> Unit and Function0<Unit>.

for a principal

Frames Unit within the type lattice and contrasts a one-value type (carries no info) with void and with Nothing.

## What Unit is `Unit` is a built-in Kotlin type that has **exactly one value** -- the singleton `object Unit`. Because there is only one possible value, returning Unit carries **no information**: callers learn nothing from it except that the function finished. It is the type used by functions that exist for their **side effects** (printing, writing to a database, mutating a variable) rather than for producing a result. ## Implicit return type and implicit return If you omit the return type, Kotlin infers `Unit`: ```kotlin fun greet(name: String) { // return type is Unit println("Hi, $name") } // Fully explicit equivalent fun greet2(name: String): Unit { println("Hi, $name") return Unit } ``` Both the `: Unit` and the `return Unit` are inserted by the compiler. You almost never write them by hand. ## Unit is a real value Unlike a keyword, `Unit` is an ordinary object you can reference: ```kotlin val x: Unit = Unit // legal val y = greet("Ann") // y has type Unit, value Unit ``` This matters when Unit appears as a **generic type argument**, e.g. a `Function0<Unit>` (a lambda returning nothing useful), or `Result<Unit>`, or `Channel<Unit>` used purely for signalling. ## Key keywords/APIs - The type `Unit` and its single value `Unit`. - Implicit `return Unit` at the end of a Unit-returning function. - Generic positions like `() -> Unit`, `Function0<Unit>`.

  • Can you store the result of a Unit-returning function in a variable?
    Yes. `val x = println("hi")` gives x type Unit and value the Unit singleton; it is legal but pointless.
  • How many instances of Unit exist?
    Exactly one -- Unit is a singleton object, so all Unit values are the same instance.

Unit is like a delivery confirmation slip: it tells you the job is done but carries no other information.

saying these in an interview costs you the question

  • Saying Unit means 'no return statement is allowed'
  • Claiming Unit is a keyword like void rather than a type with a value
  • Thinking you must explicitly write `return Unit`
  • Saying Unit can have many values
  • Confusing Unit with null

context

open as a page

Contrast Unit with Nothing. When does a function return Unit versus Nothing, and how does each affect control flow?

level: middleimportance: must knowfreq 55%

basics

~10 s

Unit means the function returns normally but with no useful value. Nothing means the function never returns at all -- it always throws or loops forever. Unit has one value; Nothing has none.

open as a page

How does Kotlin's Unit differ from Java's void, both conceptually and at the bytecode level?

level: middleimportance: should knowfreq 60%

basics

~20 s

Java's void is not a type and has no value. Kotlin's Unit is a real type with one value, so it can be used in generics. On the JVM, Unit-returning functions usually still compile to void methods.

open as a page

Explain Unit's role in lambdas and SAM/functional-type conversions: the last-expression coercion to Unit and why () -> Unit lambdas don't need an explicit return.

level: seniorimportance: should knowfreq 45%

basics

~20 s

When a lambda's expected type returns Unit, Kotlin ignores whatever the last expression produces and treats the result as Unit. So you never need to add return Unit or worry that the last line returns something else.

open as a page

When designing generic APIs and Java interop, when should you pick Unit as a type argument, and what pitfalls arise (e.g. Unit vs Void, suspend functions, Continuation)?

level: principalimportance: nice to knowfreq 30%

basics

~20 s

Use Unit as a generic argument when an API needs a type but there's no meaningful payload, like a result that only signals success. Watch out for Java interop where Unit maps to kotlin.Unit, not java.lang.Void, and for suspend functions whose Unit result still flows through Continuation.

open as a page