Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: July 27, 2024
Kotlin, being a prominent programming language owing to its simplicity and expressiveness, introduces some unit concepts such as Any, Unit, and Nothing. Simply put, these concepts are part of Kotlin’s type system, and understanding them is crucial to writing modular and effective Kotlin code.
In this tutorial, we’ll explore these concepts and demonstrate their use with some Kotlin code.
The Any class in Kotlin is the root of Kotlin’s class hierarchy. It represents any non-null types or objects. This means that every type in Kotlin is derived from Any, making it a suitable choice for variables that can hold any type of value.
To demonstrate the flexibility and utility of the Any type, we’ll present a simple Kotlin method that accepts a parameter of type Any and returns it:
fun printAnyType(value: Any): Any {
return value
}
For purposes of correctness, let’s now unit-test this method:
@Test
fun `prints value for any type`() {
val intValue = 14
val stringValue = "Hello Kotlin"
assertEquals("Hello Kotlin", printAnyType(stringValue))
assertEquals(14, printAnyType(intValue))
}
This unit test verifies that our method correctly handles different input types. It does this by passing various inputs to the method and asserting that the expected values are returned.
The Any class is a core concept of Kotlin and has some important advantages. However, there are some trade-offs that we need to be aware of as well. Knowing these helps us to use Any effectively.
Here are some advantages of using the Any class:
Let’s also look at some disadvantages of using this class:
In Kotlin, Unit is a type that corresponds to the void type in Java. We often use it as the return type of a method that does not return any meaningful value. However, unlike void, Unit is a real type and we can use it as a return type for our methods. This can be particularly useful for methods that perform side effects, such as logging or updating a user interface
Let’s look at how Unit is used in Kotlin through a simple example:
fun performTask(): Unit {
println()
}
This method performs a task that doesn’t return any actual value and as such, it returns Unit.
Now, let’s unit-test this method:
@Test
fun `shows how to use unit type`() {
val result = performTask()
assertNotNull(result)
}
From this unit test, we observe that we capture the returned value from calling our helper method and assert that the value is not null.
In Kotlin, the Unit type represents the lack of a significant return value, akin to void in languages such as Java. Despite being an actual type, Unit comes with its own set of benefits and drawbacks. Familiarizing ourselves with these aspects can enable us to utilize Unit effectively in our Kotlin applications.
Let’s look at some advantages of using the Unit class:
Also, let’s glance at some disadvantages of using the Unit class:
The Nothing type in Kotlin represents a value that never exists. We use it to indicate that a method does not return under any circumstances, such as when it throws an exception or enters an infinite loop:
fun throwError(message: String): Nothing {
throw IllegalArgumentException(message)
}
In this method, the throwError() method explicitly declares a return type of Nothing to indicate that it never returns normally, but instead always throws an exception.
As usual, we need to test this method for correctness:
@Test
fun `shows how to use nothing type`() {
assertFailsWith<IllegalArgumentException> {
throwError("Invalid argument")
}
}
In this unit test, we are asserting that the throwError() method indeed throws an IllegalArgumentException with the specified message, showing that our method executed as expected.
The Nothing type can be useful for methods that always throw an exception or for representing unreachable code. Similarly, understanding the advantages and disadvantages of using Nothing can us you leverage its power effectively.
Here are some advantages of using the Nothing class:
Here are some disadvantages of using this class:
In this article, we’ve delved into three fundamental Kotlin types: Any, Unit, and Nothing. Any allows for flexible methods by serving as the root of the Kotlin class hierarchy, though it may introduce performance overhead. Unit, akin to Java’s void, is useful for side-effect methods, enhancing readability and type safety. Nothing indicates methods that never return, aiding in explicit error handling and maintaining type safety.