k314159
k314159

Reputation: 11182

BigInteger as kotlin.Number

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

Upvotes: 0

Views: 710

Answers (1)

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)

Upvotes: 3

Related Questions