How does a Kotlin function that returns Unit appear when called from Java, and what is the JVM bytecode return type?
answer
- Unit return type ⇒ bytecode void (V)
- Unit is a real type, single value Unit.INSTANCE
- void cannot be a generic arg ⇒ Function0<Unit> needs Unit.INSTANCE
- Java sees void greet(), not Unit greet()
basics
~10 sA Kotlin function returning Unit looks like a normal void method in Java. The JVM return type is void, so Java callers just call it and get nothing back.
solid answer
~30 sKotlin's Unit is a real type with a single value (the Unit.INSTANCE singleton), but when it is the declared return type of a function, the Kotlin compiler emits a JVM method with return type void. So from Java a `fun greet(): Unit` is just `void greet()` — there is no Unit value to handle, and Java code calls it like any void method. The Unit object only materializes when Unit is used in a generic/functional position (e.g. a `Function0<Unit>`), where void cannot appear and Kotlin must return Unit.INSTANCE. For a plain top-level or member function the bytecode descriptor ends in `)V`.
code
kotlin · 5 linesfun log(msg: String): Unit { println(msg) } // bytecode: void log(String)
val action: () -> Unit = { println("run") } // Function0<Unit>, invoke returns Unit.INSTANCE
// From Java:
// obj.log("hello"); // void call
// Function0<Unit> a = () -> { ...; return Unit.INSTANCE; };go deeper
Knows Unit ≈ void and that Java sees a void method; can call it normally.
Explains the bytecode descriptor V and that Unit is a real singleton type.
Distinguishes return-position (void) from generic/functional position (Unit.INSTANCE) and explains why.
Connects this to JVM generic erasure constraints and the design rationale for a uniform single-value Unit type vs Java's special-cased void.
## What `Unit` is `Unit` is Kotlin's type for "this function returns no meaningful value" — the rough equivalent of `void` in Java. Unlike Java's `void`, `Unit` is a **real type** with exactly **one value**, accessed as `Unit` (the `Unit.INSTANCE` singleton in Java). Every Kotlin function has a return type; functions that would be `void` in Java have return type `Unit`, which can be omitted in source. ## How it compiles When `Unit` is the **declared return type of a function**, the compiler emits a JVM method whose return descriptor is `V` (void). So: ```kotlin fun greet(name: String): Unit { println("hi $name") } // or, equivalently, with the type omitted: fun greet2(name: String) { println("hi $name") } ``` both compile to `public void greet(String)`. From Java you call `obj.greet("x");` — there is **no return value** to capture. ## When the `Unit` object appears The `Unit.INSTANCE` singleton only materializes in **functional / generic positions**, where the JVM has no `void` type for a type argument. A `() -> Unit` lambda becomes a `Function0<Unit>`, and its `invoke()` must `return Unit.INSTANCE;`. Java code wiring such a lambda must explicitly return that: ```java Function0<Unit> f = () -> { System.out.println("x"); return Unit.INSTANCE; }; ``` ## Summary - Plain `Unit`-returning function ⇒ bytecode `void`, called normally from Java. - `Unit` as a **type argument** ⇒ the real `Unit.INSTANCE` object is required. - `Unit` is referenced from Java as `kotlin.Unit`.
- Why can't the compiler just use void for a () -> Unit lambda?JVM generics are reference-type only; `void` is not a type and cannot be a type argument. The function interface is `Function0<R>`, so R must be a real type — Kotlin uses `Unit` and returns `Unit.INSTANCE`.
- How do you reference Unit from Java code?As `kotlin.Unit`, and its singleton as `kotlin.Unit.INSTANCE`.
Unit is like a sealed empty box: at the door (function return) it's flattened to 'nothing' (void), but inside a container slot (generic) you still need the box object.
saying these in an interview costs you the question
- Claiming Unit-returning functions compile to a method returning a Unit object instead of void
- Saying Unit and Java void are identical (Unit is a real type with a value)
- Thinking you must return something from a void Kotlin function in Java
- Believing a () -> Unit lambda compiles to a void-returning JVM method