<分区>
不是应该在幕后调用equals吗?
scala> 1 equals 1l
res2: Boolean = false
scala> 1 == 1l
res3: Boolean = true
标签 scala scala-2.10
<分区>
不是应该在幕后调用equals吗?
scala> 1 equals 1l
res2: Boolean = false
scala> 1 == 1l
res3: Boolean = true
最佳答案
来自 Programming in Scala 2nd Edition , 第 11 章
1 The only cases where == is does not directly call equals is for Java’s boxed numeric classes such as Integer or Long . In Java, a new Integer(1) does not equal a new Long(1) even though for primitive values 1 == 1L . Since Scala is a more regular language than Java it was necessary correct this discrepancy by special-casing the == method for these classes. Likewise, the ## method provides a Scala version of hashing that is the same as Java’s hashCode , except for boxed numeric types, where it works consistently with == . For in- stance new Integer(1) and new Long(1) hash the same with ## even though their Java hashCode s are different.
关于scala - 为什么 Scala 的 == 在 Int 的情况下不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230539/