Kotlin: Understanding 'nothing' and 'void'
In the realm of programming, the terms 'nothing' and 'void' often cause confusion, even among seasoned developers. While both are used to represent the absence of a value, they serve different purposes in Kotlin, a modern statically-typed programming language. Let's delve into the intricacies of 'nothing' and 'void' in Kotlin, exploring their unique characteristics and use cases.
'Nothing' in Kotlin
'Nothing' in Kotlin is a type that represents the absence of a value. It's used when a function doesn't return anything, or when a property doesn't hold any value. 'Nothing' is a subtype of every other type, meaning it can be assigned to any variable, but no value can be assigned to it.
Here's a simple example of 'nothing' in action:

fun printHelloWorld(): Nothing {
throw RuntimeException("This function doesn't return anything")
}
fun main() {
printHelloWorld()
}
In this example, the 'printHelloWorld' function doesn't return anything, so its return type is 'Nothing'. When we call this function in the 'main' function, it throws an exception, demonstrating that it doesn't produce any output.
Use Cases of 'Nothing'
- Error-throwing functions: 'Nothing' is often used in functions that always throw an exception, like the 'printHelloWorld' example above.
- Suspension points: In Kotlin's coroutines, 'Nothing' is used to mark the end of a suspension sequence.
- Extension functions: 'Nothing' can be used as the return type of extension functions to indicate that they don't change the receiver object.
'Void' in Kotlin
'Void' in Kotlin is a type that represents the absence of a value, similar to 'nothing'. However, 'void' is used specifically for interoperability with Java. When you're working with Java libraries or APIs that return 'void', you can use 'kotlin.Unit' (which is the Kotlin equivalent of Java's 'void') to maintain type safety.
Here's an example of using 'kotlin.Unit' to mimic Java's 'void':

fun printMessage(message: String): kotlin.Unit {
println(message)
}
In this example, the 'printMessage' function doesn't return anything, so its return type is 'kotlin.Unit'. This is equivalent to Java's 'void'.
Use Cases of 'Void'
- Interoperability with Java: 'kotlin.Unit' is used to maintain type safety when working with Java libraries or APIs that return 'void'.
- Empty functions: 'kotlin.Unit' can be used as the return type of empty functions that don't perform any operations.
Comparing 'Nothing' and 'Void'
While both 'nothing' and 'void' represent the absence of a value, they serve different purposes in Kotlin. 'Nothing' is a more general concept, used to indicate the absence of a value in various contexts. On the other hand, 'void' (or 'kotlin.Unit') is specifically used for interoperability with Java.
| Aspect | 'Nothing' | 'Void' (kotlin.Unit) |
|---|---|---|
| Purpose | Represents the absence of a value in general | Used for interoperability with Java |
| Subtyping | Subtype of every other type | Not a subtype of any other type |
| Use cases | Error-throwing functions, suspension points, extension functions | Interoperability with Java, empty functions |
Understanding the nuances of 'nothing' and 'void' in Kotlin is crucial for writing efficient, type-safe, and maintainable code. By leveraging these concepts effectively, you can enhance your Kotlin development experience and create powerful, expressive applications.





















