One would expect that even though strings are immutable, value-equality and reference-equality would not be the same for java.lang.String objects in Scala. This means that two string-holding val
s should not be reference-equal even when their strings are identical. But here's what I get in a 2.9.1.final REPL:
scala> val s1 = "a"; val s2 = "a"
s1: java.lang.String = a
s2: java.lang.String = a
scala> s1 eq s2
res0: Boolean = true
Any idea why the result was not false
? The same experiment with List("a")
instead of "a"
works as expected. The eq
method is marked as final in AnyRef. Is there any compiler magic done specifically for String
or java.lang.String
?