Kotlin学习笔记-数据类型 数值类型转换(3)

java中数值类型 范围小的类型赋值给范围大的类型可以隐式转换
kotlin不可以

Kotlin学习笔记-数据类型 数值类型转换(3)_第1张图片
image.png


Kotlin学习笔记-数据类型 数值类型转换(3)_第2张图片
image.png
/**
 * Superclass for all platform classes representing numeric values.
 */
public abstract class Number {
    /**
     * Returns the value of this number as a [Double], which may involve rounding.
     */
    public abstract fun toDouble(): Double

    /**
     * Returns the value of this number as a [Float], which may involve rounding.
     */
    public abstract fun toFloat(): Float

    /**
     * Returns the value of this number as a [Long], which may involve rounding or truncation.
     */
    public abstract fun toLong(): Long

    /**
     * Returns the value of this number as an [Int], which may involve rounding or truncation.
     */
    public abstract fun toInt(): Int

    /**
     * Returns the [Char] with the numeric value equal to this number, truncated to 16 bits if appropriate.
     */
    public abstract fun toChar(): Char

    /**
     * Returns the value of this number as a [Short], which may involve rounding or truncation.
     */
    public abstract fun toShort(): Short

    /**
     * Returns the value of this number as a [Byte], which may involve rounding or truncation.
     */
    public abstract fun toByte(): Byte
}

你可能感兴趣的:(Kotlin学习笔记-数据类型 数值类型转换(3))