does Any == Object

The following code in kotlin:

Any().javaClass

Has value of java.lang.Object. Does that mean Any and Object are the same class? What are their relations?


Solution 1:

No.

From the Kotlin docs (Emphasis mine)

All classes in Kotlin have a common superclass Any, that is a default super for a class with no supertypes declared:

class Example // Implicitly inherits from Any

Any is not java.lang.Object; in particular, it does not have any members other than equals(), hashCode() and toString(). Please consult the Java interoperability section for more details.

Further, from the section on mapped types we find:

Kotlin treats some Java types specially. Such types are not loaded from Java “as is”, but are mapped to corresponding Kotlin types. The mapping only matters at compile time, the runtime representation remains unchanged. Java’s primitive types are mapped to corresponding Kotlin types (keeping platform types in mind):

...

java.lang.Object kotlin.Any!

This says that at runtime java.lang.Object and kotlin.Any! are treated the same. But the ! also means that the type is a platform type, which has implication with respect to disabling null checks etc.

Any reference in Java may be null, which makes Kotlin’s requirements of strict null-safety impractical for objects coming from Java. Types of Java declarations are treated specially in Kotlin and called platform types. Null-checks are relaxed for such types, so that safety guarantees for them are the same as in Java (see more below).

...

When we call methods on variables of platform types, Kotlin does not issue nullability errors at compile time, but the call may fail at runtime, because of a null-pointer exception or an assertion that Kotlin generates to prevent nulls from propagating:

Solution 2:

Kotlin compiler treats kotlin.Any and java.lang.Object as two different types, but at runtime they are represented with the same java.lang.Object class.

javaClass property returns the runtime class of an instance, so that's why you get the same java.lang.Object class in both cases.

There are also other types which are different at compile time, but the same at runtime; they are listed in the Mapped types section of the documentation.

Solution 3:

In fact, there is no java.lang.Object in aspect of Kotlin lang, so you certainly cannot say Any is Object. You can only say Any takes the place of Object in Kotlin.

Solution 4:

It's exactly the same. It just blocks the attributes that Kotlin doesn't want:

fun main() {
    val any = Any()//java Object
    val obj = Object()//java Object
    println(any::class)//Object class
    println(obj::class)//Object class
}

smali:

fun test(aaaaa: Any) {
}
  public final static test(Ljava/lang/Object;)V
    // annotable parameter count: 1 (visible)
    // annotable parameter count: 1 (invisible)
    @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
   L0
    ALOAD 0
    LDC "aaaaa"
    INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkNotNullParameter (Ljava/lang/Object;Ljava/lang/String;)V
   L1
    LINENUMBER 4 L1
    RETURN
   L2
    LOCALVARIABLE aaaaa Ljava/lang/Object; L0 L2 0
    MAXSTACK = 2
    MAXLOCALS = 1

  @Lkotlin/Metadata;(mv={1, 4, 2}, bv={1, 0, 3}, k=2, d1={"\u0000\u000e\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0000\n\u0000\u001a\u000e\u0010\u0000\u001a\u00020\u00012\u0006\u0010\u0002\u001a\u00020\u0003\u00a8\u0006\u0004"}, d2={"test", "", "aaaaa", "", "SPWrapper.app"})
  // compiled from: T.kt
}