skip to content

Any & Any? at the Top

Any is the root of every non-null type and Any? the root of all types, carrying only equals, hashCode, and toString. The comparison worth making is with java.lang.Object: Any has fewer members, and wait/notify are not among them.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

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

open as a page

`Any` declares `equals` and `hashCode`. What is the contract between them, and how does a Kotlin `data class` satisfy it?

level: middleimportance: must knowfreq 75%

basics

~10 s

If two objects are equal by equals, they must return the same hashCode. data class auto-generates both from the constructor properties, so equal data objects always share a hash code.

open as a page

How does `kotlin.Any` differ from `java.lang.Object`? What happens to the extra `Object` methods when running on the JVM?

level: middleimportance: should knowfreq 55%

basics

~10 s

Any is Kotlin's root and only promises equals, hashCode, toString. Object has extra methods like wait, notify, getClass. On the JVM Any maps to Object, so those extras still exist but Kotlin hides them.

open as a page

What is the difference between a generic parameter `<T>` and `<T : Any>`? When would you use the `Any` upper bound?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A plain <T> actually means <T : Any?>, so T may be nullable. Writing <T : Any> forces T to be a non-null type, banning String? and friends — useful when null would break your logic.

open as a page

When designing a public type, what subtle pitfalls arise around the `Any` members `equals`, `hashCode`, and `toString` — especially for sensitive data and arrays?

level: seniorimportance: nice to knowfreq 35%

basics

~10 s

Default toString leaks little, but data class toString prints every field — including secrets. Arrays compare by identity, not content. And mutable fields used in hashCode corrupt hash-map keys. Override these members carefully.

open as a page