Menu

Kotlin Any, Unit and Nothing Explained Simply
📰

Kotlin Any, Unit and Nothing Explained Simply

DEV Community: kotlin·Mayank Kumar·6 days ago
#eGUMEjyI
#devtokotlin#devto#dev#nothing#type
Reading 0:00
15s threshold

In short: Any is the parent of every non-null type, so it can hold any value. Unit means "the work is done, there is no result to give back". Nothing means "this code never finishes normally", for example because it always throws.

Open a typical Android screen written in Kotlin and you will often find all three. A button takes onClick: () -> Unit . A loading state is UiState<Nothing> . An analytics call takes values of type Any? . They look like small details, but each one tells the compiler something useful.

This post covers what each type means, how it differs from Java, and where you use it in real Android code.

Where do Any, Unit and Nothing fit in Kotlin?

Think of Kotlin's types as a ladder.

At the top is Any? . Every value fits here, even null .

Just below is Any . Every value except null fits here.

In the middle are the types you use every day: built-in ones like String and Int , your own classes like Order (from the food-order app in this series), and also Unit .

At the bottom is Nothing .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.