kotlin int盒装标识 [英] kotlin int boxed identity
本文介绍了kotlin int盒装标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我们的文档中
请注意,数字装箱不会保留身份
Note that boxing of numbers does not preserve identity
但下一个示例给出不同的结果
but the next examples give different results
val number1 = 127
val b1 : Int? = number1
val b2 : Int? = number1
print(b1 === b2) // this prints true
val number2 = 128
val c1 : Int? = number2
val c2 : Int? = number2
print(c1 === c2) // this prints false
数字大于127的工作符合预期,但是当大于128(8位)时,为什么?
In numbers greater than 127 works as expected but not when is above 128 (8 bits), why?
推荐答案
本文解释了它:< a href =http://javapapers.com/java/java-integer-cache/ =noreferrer> http://javapapers.com/java/java-integer-cache/
基本思想是Java标准库对-128到127之间的值使用缓存,因此它们总是引用相同的Integer对象(通过标识)。
The basic idea is that the Java standard lib uses a cache for values between -128 and 127, therefore they always refer to the same Integer object (by identity).
这篇关于kotlin int盒装标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文