Java has Object to represent the root of the class hierarchy and void to represent the lack of a type. We will see here how Kotlin represent these types and how it improves them.

Any

Object is the root of the class hierarchy in Java, every class has Object as a superclass. In Kotlin the Any type represents the super type of all non-nullable types.

It differs to Java’s Object in 2 main things:

In Java, primitives types aren’t type of the hierarchy and you need to box them implicitly, while in Kotlin Any is a super type of all types.

is a super type of all types. Any can’t hold the null value, if you need null to be part of your variable you can use the type Any?

java.lang.Object methods toString, equals and hasCode are inherited from Any while to use wait and notify you will need to cast your variable to Object to use them.

Unit

In Java if we want that a function does return nothing we use void , Unit is the equivalent in Kotlin.

The main characteristics of Unit against Java’s void are:

Unit is a type and therefore can be used as a type argument.

is a type and therefore can be used as a type argument. Only one value of this type exists.

It is returned implicitly. No need of a return statement.

Nothing

This type does not exist in Java. It is used when a function will never terminate normally and therefore a return value has no sense.

It is very useful when analyzing this type of code, to know that the function will never terminate.

An example of this kind of functions are the fail function in test systems, or the main loop in a game engine.

Conclusion