One would expect that even if the strings are immutable, the equivalence of equality and reference equality will not be the same for java.lang.String objects in Scala. This means that the two holding lines valshould not be referenced, even if their lines are identical. But here is what I get in 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 "a"works as expected. The method is eqmarked final in AnyRef . Is there any compiler magic specifically for Stringor java.lang.String?
source
share