Kotlin int boxed identity

The documentation has

Note that boxing numbers does not store identifier

but the following 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 

In numbers greater than 127, it works as expected, but not when it is above 128 (8 bits), why?

+5
source share
1 answer

This article explains this: http://javapapers.com/java/java-integer-cache/

The main idea is that the standard Java library uses a cache for values ​​from -128 to 127, so they always refer to the same Integer object (by identifier).

+8
source

Source: https://habr.com/ru/post/1260790/


All Articles