The question for basic type


#1

In the reference.I saw this code.
val a: Int = 10000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA === anotherBoxedA) // !!!Prints ‘false’!!!

When I run this code in my intellij idea. I get the result ‘false’.
But when I modify the code like below.
val a: Int = 1
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA === anotherBoxedA)
I got the result ‘true’.

Who can tell me why? Or is this a bug?


#2

This is a Java trick, you response is here:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java#Integer.valueOf(int)

Some int values (-128…127) are cached, others are instantiated every time.

This behaviour is unspecified and subject to change, so use “==” instead “===”.


#3

Thank you very much.
I will memorize this.