Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does Any == Object

Tags:

kotlin

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?

like image 262
oshai Avatar asked Aug 04 '16 07:08

oshai


People also ask

What does type any mean?

any: It is a built-in data type in TypeScript which helps in describing the type of variable which we are unsure of while writing the code. Though the data type 'any' is useful it shouldn't be used unnecessarily. Syntax: var x: any; // This means x has no interface.

Is there object type in TypeScript?

TypeScript 2.2 introduced a new type called object . It represents any non-primitive type. The following types are considered to be primitive types in JavaScript: string.

What is any [] in TypeScript?

Types Array<any> and any[] are identical and both refer to arrays with variable/dynamic size. Typescript 3.0 introduced Tuples, which are like arrays with fixed/static size, but not really.

What is the use of any in angular?

If you use any instead you are basically telling the transpiler that anything goes, you are providing no information about what is stored in a - it can be anything! And therefore the transpiler will allow you to do whatever you want with something defined as any .


2 Answers

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:

like image 60
Michael Anderson Avatar answered Sep 28 '22 02:09

Michael Anderson


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.

like image 24
Ilya Avatar answered Sep 28 '22 00:09

Ilya