企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
### 数值类型 [TOC] #### 数字自动装箱-表示方式 Kotlin数字在虚拟机中是物理存储为JVM原生类型,但如果是**可null引用(有问号?)或者泛型对象**, 那么kotlin数字就会自动装箱(类似java包装类,如int自动装箱转为Integer) >[info] 在Kotlin中,存在数字的装箱,但是不存在拆箱 **kotlin数字自动装箱的实例:** ~~~ val a: Int = 10000 val boxA: Int? = a //有问号? 会自动装箱 val boxB: Int? = a //有问号? 会自动装箱 print(boxA === boxB) //输出false,如果数值在-128和127之间,输出true print(boxA == boxB) //输出true ~~~ **两个数值的比较:==和===** * kotlin三个等号===:比较的是对象地址(引用),自动装箱会生成新对象,两个对象不同,输出false;但是数值在-128和127之间时,自动装箱会重用同一个对象(java缓存机制),输出true * kotlin两个等号==:比较的是对象数值,两个对象数值相同,输出true 也可参考kotlin官方参考文档给的示例 **三个等号===** ~~~ val a: Int = 10000 print(a === a) // 输出“true” val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA === anotherBoxedA) // !!!输出“false”!!! ~~~ **两个等号==** ~~~ val a: Int = 10000 print(a == a) // 输出“true” val boxedA: Int? = a val anotherBoxedA: Int? = a print(boxedA == anotherBoxedA) // 输出“true” ~~~ #### 显式转换 **范围较小数字类型不能隐式转换为范围较大类型**。 (引申义:可以显示转换) ~~~ val b: Byte = 1 // 正确, 字面值是静态检测的 val i: Int = b // 错误: 隐式扩宽 val i: Int = b.toInt() // 正确: 显式扩宽 ~~~ 每个数字类型支持如下类型转换: ~~~ toByte(): Byte toShort(): Short toInt(): Int toLong(): Long toFloat(): Float toDouble(): Double toChar(): Char ~~~ 示例: ~~~ fun main(args: Array<String>) { var numA: Int = 97 println(numA.toByte()) println(numA.toShort()) println(numA.toInt()) println(numA.toLong()) println(numA.toFloat()) println(numA.toDouble()) println(numA.toChar()) println(numA.toString()) } ~~~ 输出结果如下 ~~~ 97 97 97 97 97.0 97.0 a 97 ~~~ #### 运算符 + 重载 **缺少隐式扩宽转换是很少引人注意的,因为类型是从上下文推断出来的,而算术运算会有重载做适当扩宽转换**,例如: ~~~ val l = 1L + 3 // Long + Int ——> Long ~~~ 这个是通过运算符 + 重载实现的。我们可以在[Long类](https://www.w3cschool.cn/doc_kotlin/kotlin-api-latest-jvm-stdlib-kotlin--long-index.html?lang=en)的源代码中看到这个 plus 运算符函数的定义: ``` public operator fun plus(other: Byte): Long public operator fun plus(other: Short): Long public operator fun plus(other: Int): Long public operator fun plus(other: Long): Long public operator fun plus(other: Float): Float public operator fun plus(other: Double): Double ``` 也就是说, 编译器会把 1L + 3 翻译成 1L.plus(3) ,然后这个传入的参数类型必须是Byte、Short、Int、Long、Float、Double中的一种。例如,我们传入一个字符 Char 参数,比如 ~~~ val l=1L+'a' ~~~ 编译器就会直接抛错: ``` Error:(26, 13) Kotlin: None of the following functions can be called with the arguments supplied: public final operator fun plus(other: Byte): Long defined in kotlin.Long public final operator fun plus(other: Double): Double defined in kotlin.Long public final operator fun plus(other: Float): Float defined in kotlin.Long public final operator fun plus(other: Int): Long defined in kotlin.Long public final operator fun plus(other: Long): Long defined in kotlin.Long public final operator fun plus(other: Short): Long defined in kotlin.Long ``` 除了这个加法运算,观察源码我还可以看到加减乘除都有对应的方法 >[info]【PS】:其实我们在idea工具中查看源码知道这些基本数据类型都在Primitives.kt文件中,在前面的数据类型和类型系统章节结尾处,可以看到各个数据类型的方法和属性。 * **Long** ![](https://box.kancloud.cn/9d895c5c47fbd747ad0c2db872cdbd76_370x670.png) ![](https://box.kancloud.cn/2498405ca1e6bd1f6e919c998b7fe42d_370x692.png) ![](https://box.kancloud.cn/d9579ad40bfc8aa81c817414897d2366_370x458.png)