Hey, could someone explain to me, why the followin...
# announcements
m
Hey, could someone explain to me, why the following prints true?
Copy code
val a = "x"
val b = "x"
println(a === b)
This differs from the behaviour that I expected, as the following prints false:
Copy code
val c = "X".toLowerCase()
println(a === c)
example code: https://pl.kotl.in/NTVgQNuWe
k
The JVM itself pools constant strings so there's only one instance per string.
m
Okay thanks, does this affect any other type?
d
'===' means referential equality and yeah what Karel said^
k
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
all primitive Java types I would say
d
Yes, for primitive types (int, long, etc.) === and == are the same