scala - 为什么 Scala 的 == 在 Int 的情况下不同?

标签 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/

相关文章:

scala - 如何为 futures、并行集合和 akka 创建共享线程池/executioncontext

scala - 为什么相同的 Scala 代码在命令行中可以正常工作,而在 Intellij 中却不行?

scala - 如何处理横切关注Scala方式

playframework - 超时后中断 Play 2 中的长时间计算

scala - 为什么 Scala 2.10 在匹配单例类型时给出 'match may not be exhaustive' 警告?

Scala如何在元组中指定元组的返回类型

scala - Play 框架中的集成测试

function - Scala 方法 = trait { ... } 含义

json - 在 Play Framework Scala 中列出 Json

scala - 在 Scala 2.10 中,如何创建给定 TypeTag 的 ClassTag