skip to content

In Kotlin, what is `Any`, and how does it relate to `Any?`? Which one is the true root of the whole type system?

level: juniorimportance: must knowfreq 70%

answer

  1. Any = root of non-null types
  2. Any? = Any + null = absolute top
  3. Int <: Any <: Any?
  4. Only 3 members: equals/hashCode/toString
  5. Any can't hold null

basics

~20 s

Any is the parent of every non-null type. Any? adds the possibility of null, so it sits above Any and is the top of the whole type system — every value of any type is an Any?.

solid answer

~30 s

`kotlin.Any` is the supertype of all non-nullable types: `Int`, `String`, your classes, etc. all ultimately extend `Any`. But `Any` cannot hold `null`. `Any?` is `Any` plus null, and it is the absolute top type — every Kotlin value, nullable or not, is assignable to `Any?`. So the lattice is: a concrete type → `Any` → `Any?`. A function returning `Any` promises non-null; `Any?` allows null. `Any` declares only three open members — `equals()`, `hashCode()`, `toString()` — so it is far slimmer than Java's `Object` (no `wait`/`notify`/`getClass`). Use `Any` when you need a heterogeneous non-null reference, `Any?` when null is legitimate.

code

kotlin · 5 lines
kotlin
val items: List<Any?> = listOf(1, "two", null, 3.0)
for (it in items) {
    // every element is at least an Any?
    println(it?.toString() ?: "<null>")
}

go deeper

for a junior

Knows Any is the base of non-null types and Any? allows null.

for a middle

States the subtype chain Int <: Any <: Any? and that Any? is the true top.

for a senior

Frames Any vs Any? as an API null-safety contract and lists the three members precisely.

for a principal

Connects this to the full lattice (Nothing at the bottom, Any? at the top) and to variance/generic bound design (<T : Any> to forbid null).

## What `Any` is `kotlin.Any` is the **root of the non-nullable type hierarchy**. Every class you declare implicitly extends `Any` if you don't name another superclass, and every built-in non-null type (`Int`, `Boolean`, `String`, `List<T>`, …) is ultimately a subtype of `Any`. So a variable of type `Any` can reference *any non-null value*. ```kotlin val a: Any = 42 // Int is a subtype of Any val b: Any = "hi" // String is a subtype of Any // val c: Any = null // ERROR: Any is non-nullable ``` ## `Any?` is the real top Kotlin distinguishes a type `T` from its **nullable** form `T?`. `Any?` means "`Any` or `null`". Because *every* value — including `null` — is an `Any?`, **`Any?` is the single top type of the entire type lattice**: ```kotlin val x: Any? = null // OK val y: Any? = 42 // OK ``` The relationship is a subtype chain: `Int <: Any <: Any?`. `Any` is a subtype of `Any?` (you can always widen a non-null value to nullable), but not vice-versa. ## The three members on `Any` `Any` declares exactly three `open` functions, and nothing else: - `equals(other: Any?): Boolean` - `hashCode(): Int` - `toString(): String` That means any value, via `Any`, supports equality, hashing, and string conversion — and nothing more. There is no `getClass`, `wait`, or `notify` directly on `Any`. ## Why the distinction matters Returning `Any` vs `Any?` is a **contract**: `Any` guarantees non-null to the caller, so they don't need a null check; `Any?` forces the caller to handle null (smart-cast, `?.`, `!!`). Picking the right one is part of designing a null-safe API.

  • Can you assign a `String?` to an `Any`?
    No — `String?` may be null, and `Any` is non-nullable, so the compiler rejects it. You'd assign it to `Any?`, or smart-cast/`!!` it to a non-null `String` first.
  • Is `Nothing` a subtype of `Any`?
    Yes. `Nothing` is the bottom type — a subtype of every type, including `Any`. So `Nothing <: Any <: Any?`.

Any is 'any real thing'; Any? is 'any real thing, or nothing at all'.

saying these in an interview costs you the question

  • Saying `Any` can hold `null`
  • Calling `Any` the top of the entire lattice (that's `Any?`)
  • Claiming `Any` has methods like `getClass()` or `wait()`
  • Thinking `Any` and `Object` are identical
  • Not knowing `Any?` is a supertype of `Any`

context