Post

Any, Unit and Nothing

This article explain basic difference between Any, Unit and Nothing from kotlin language.

Any

Any is by default the superclass of all the classes and has 3 functions: equals, hashCode and toString. This is equal to Object class in Java. We can create an object of Any class directly or even override these functions in any other class.

1
2
3
4
5
public open class Any {
     public open operator fun equals(other: Any?): Boolean
     public open fun hashCode(): Int
     public open fun toString(): String
}

Unit

Unit class is a singleton class and also is an object, we can’t extend or even create an another object of it. Unit class in equal to void type in Java. The superclass of Unit is Any and it has overridden toString method.

1
2
3
public object Unit {
    override fun toString() = "kotlin.Unit"
}

Nothing

Nothing is non-open (final class) which can’t be extended and its constructor is also private that means we can’t create the object also. This is usually used to represent the return type of function which will always throw an exception. The superclass of Nothing is Any.

1
public class Nothing private constructor()
This post is licensed under CC BY 4.0 by the author.