Kotlin – Void vs. Unit vs. Nothing

The Void type is from Java. You generally won’t use this from Kotlin unless you’re using some Java-library that uses it.

The Unit type is what you return from a function that doesn’t return anything of interest. Such a function is usually performing some kind of side effect. The unit type has only one possible value, which is the Unit object. You use Unit as a return type in Kotlin when you would use void (lowercase v) in Java.

The Nothing type has no values. If a function has return type Nothing, then it cannot return normally. It either has to throw an exception, or enter an infinite loop. Code that follows a call to a function with return type Nothing will be marked as unreachable by the Kotlin compiler.

Because Nothing has no values, Nothing? is actually the type that captures only the null value in Kotlin.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)