https://kotlinlang.org logo
Title
m

molikuner

11/29/2019, 9:53 AM
Hey, could someone explain to me, why the following prints true?
val a = "x"
val b = "x"
println(a === b)
This differs from the behaviour that I expected, as the following prints false:
val c = "X".toLowerCase()
println(a === c)
example code: https://pl.kotl.in/NTVgQNuWe
k

karelpeeters

11/29/2019, 9:54 AM
The JVM itself pools constant strings so there's only one instance per string.
m

molikuner

11/29/2019, 9:55 AM
Okay thanks, does this affect any other type?
d

Dias

11/29/2019, 9:55 AM
'===' means referential equality and yeah what Karel said^
k

karelpeeters

11/29/2019, 9:57 AM
It also works for boxed integers between -128 and 127, but that's not the JVM that's the
java.lang.Integer
class keeping a cache for the factory function.
d

Dias

11/29/2019, 9:57 AM
all primitive Java types I would say
d

diesieben07

11/29/2019, 10:01 AM
Yes, for primitive types (int, long, etc.) === and == are the same