BigInteger 作为 kotlin.Number

BigInteger as kotlin.Number

在 Kotlin 中,BigInteger 来自包 java.math。那么我怎么能做到这一点:

val x: Number = BigInteger.ONE

在上面的语句中,Numberkotlin.Number,我看不出它与 java.math.BigInteger 之间有任何关系。

反之,为什么下面是编译时错误?

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

Kotlin docs on Java interop所述:

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 根本不应该在 Kotlin 中使用(与所有其他映射类型一样)。应改用其映射类型(在本例中为kotlin.Number