Home > Enterprise >  BigInteger as kotlin.Number
BigInteger as kotlin.Number

Time:11-12

In Kotlin, BigInteger comes from package java.math. So how come I can do this:

val x: Number = BigInteger.ONE

In the above statement, Number is kotlin.Number and I don't see any relationship between that and java.math.BigInteger.

Conversely, why is the following a compile-time error?

val x: java.lang.Number = BigInteger.ONE

CodePudding user response:

As Kotlin docs on Java interop states:

Kotlin treats some Java types specifically. 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.lang.Number class shouldn't be used in Kotlin at all (as all other mapped types). Its mapping type should be used instead (kotlin.Number in this case)

  • Related