How does 'Unit' differ from 'Void' in Java and 'Unit' in Kotlin?

Responsive Ad Header

Question

Grade: Education Subject: Support
How does 'Unit' differ from 'Void' in Java and 'Unit' in Kotlin?
Asked by:
64 Viewed 64 Answers

Answer (64)

Best Answer
(331)
'Void' in Java signifies that a method doesn't return anything. In Kotlin, 'Unit' is more than just the absence of a return value; it's a distinct type. 'Unit' is a value type, meaning it's immutable and can be assigned to variables. 'Void' is a keyword that indicates the absence of a return type, while 'Unit' is a type itself.