test opty
11/09/2024, 9:52 AMfalse
in code below
fun main() {
val a: Int = 1000
val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA === anotherBoxedA) // false
}
?ephemient
11/09/2024, 1:32 PMvalue can be represented by many different instances, and
===` compares whether two references are the same instanceephemient
11/09/2024, 1:33 PMString() == String()
String() !== String()
ephemient
11/09/2024, 1:35 PMInt
, it is represented as a primitive where possible, but the compiler will auto-box it (using valueOf) when neededephemient
11/09/2024, 1:37 PMInteger(0) == Integer(0)
Integer(0) !== Integer(0)
ephemient
11/09/2024, 1:38 PMInteger.valueOf(0) === Integer.valueOf(0)
while
Integer.valueOf(1000) !== Integer.valueOf(1000)
and that is because of the small integer cacheephemient
11/09/2024, 1:41 PM==
is Java's .equals()
and Kotlin's ===
is Java's ==
, but Kotlin generally has the same operational semantics as Java for the same structures (as it most commonly runs on JVM, and even Kotlin Multiplatform usually has similar behavior)Shawn
11/19/2024, 4:44 PM===
check)ephemient
11/19/2024, 4:53 PMa
is not a reference, but boxedA
and anotherBoxedA
are, so it gets compiled into
Integer boxedA = Integer.valueOf(a);
Integer anotherBoxedA = Integer.valueOf(a);
Shawn
11/19/2024, 5:17 PMShawn
11/19/2024, 5:19 PMInteger
instances (and then therefore would be references), but I suppose there must be optimizations around primitive type usage on the JVM that allow this to happenephemient
11/19/2024, 5:19 PMephemient
11/19/2024, 5:23 PM