0

I have a question about the basic syntax of kotlin. In kotlin, if you substitute a basic type to a nullable type, you will get boxing. The perception is that they are not considered identical. However, in the byte range (-128 to 127), they are considered identical, and I looked into the cause of the problem and could not find any helpful information. If there is someone who knows, please teach me.

This is the official site. https://kotlinlang.org/docs/reference/basic-types.html

var a: Int = 127
var numA: Int? = a
var numB: Int? = a
println(numA === numB) // true
Severge11
  • 11
  • 2
  • The JVM caches these values for performance reasons. https://stackoverflow.com/questions/3130311/weird-integer-boxing-in-java – Tenfour04 Jul 28 '20 at 18:27

1 Answers1

0

The docs say that it does not necessarily preserve identity and gives examples of where it does. I would guess that for the byte range, there are predefined constants that are used to reduce object churn. The Java Integer class does a similar thing for the same range as this is part of the Java Language Specification.

Don Hosek
  • 981
  • 6
  • 23