Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Any is the root of the Kotlin class hierarchy. Every non-nullable type in Kotlin implicitly inherits from Any, making it the ultimate superclass for all non-nullable objects, including user-defined classes, standard library types, and boxed primitives.

Type Hierarchy: Any vs Any?

Because Kotlin enforces null safety at the type-system level, there is a strict distinction between Any and its nullable counterpart, Any?.
  • Any: The top type for all non-nullable types. A variable of type Any is guaranteed to hold an instance of an object and can never be null.
  • Any?: The absolute top type (universal supertype) in Kotlin. It is the supertype of all types, including Any itself, and can hold both object instances and null references.
val nonNullableRoot: Any = "Kotlin" 
// val invalidRoot: Any = null      // Compilation error: Null can not be a value of a non-null type Any

val absoluteRoot: Any? = null       // Valid
val absoluteRootAlso: Any? = 42     // Valid

API Surface

The Any class exposes a minimal API footprint. It defines exactly three open methods, establishing the foundational contract for object equality, hashing, and string representation across the language.
public open class Any {
    /**
     * Indicates whether some other object is "equal to" this one.
     */
    public open operator fun equals(other: Any?): Boolean

    /**
     * Returns a hash code value for the object.
     */
    public open fun hashCode(): Int

    /**
     * Returns a string representation of the object.
     */
    public open fun toString(): String
}
Because these methods are declared as open, any Kotlin class can override them to provide custom implementations. The equals method is marked with the operator modifier, allowing it to be invoked implicitly using the structural equality operator (==).

JVM Mapping and java.lang.Object

When compiling for the Java Virtual Machine (JVM), Kotlin maps Any directly to java.lang.Object at runtime. However, at compile time, Kotlin’s Any intentionally strips away the concurrency and lifecycle methods present in Java’s Object. Methods such as wait(), notify(), notifyAll(), clone(), and finalize() are not part of the Any API. This design choice makes the Kotlin standard library platform-agnostic, allowing it to compile to JavaScript (Kotlin/JS) or native binaries (Kotlin/Native) where JVM threading concepts do not apply. If access to these JVM-specific methods is strictly required in a Kotlin/JVM project, the Any reference must be explicitly cast to java.lang.Object. Furthermore, standard JVM concurrency rules apply, meaning monitor methods like wait() must be called from within a synchronized block.
val obj: Any = Any()

// obj.wait() // Compilation error: Unresolved reference: wait

// Explicit cast and synchronization required to access java.lang.Object monitor methods
synchronized(obj) {
    (obj as java.lang.Object).wait()
}
Similarly, Java’s getClass() method is not available on Any. Type introspection in Kotlin is handled via the ::class syntax, which returns a KClass instance, or ::class.java to obtain the underlying Java Class instance.
Master Kotlin with Deep Grasping Methodology!Learn More