What is the difference between `##` and `hashCode`?

前端 未结 4 1206
陌清茗
陌清茗 2020-11-30 04:53

What is the difference between methods ## and hashCode?

They seem to be outputting the same values no matter which class or hashCode

4条回答
  •  借酒劲吻你
    2020-11-30 05:00

    Just want to add to the answers of other posters that although the ## method strives to keep the contract between equality and hash codes, it is apparently not good enough in some cases, like when you are comparing doubles and longs (scala 2.10.2):

    > import java.lang._
    import java.lang._
    
    > val lng = Integer.MAX_VALUE.toLong + 1
    lng: Long = 2147483648
    
    > val dbl = Integer.MAX_VALUE.toDouble + 1
    dbl: Double = 2.147483648E9
    
    > lng == dbl
    res65: Boolean = true
    
    > lng.## == dbl.##
    res66: Boolean = false
    
    > (lng.##, lng.hashCode)
    res67: (Int, Int) = (-2147483647,-2147483648)
    
    > (dbl.##, dbl.hashCode)
    res68: (Int, Int) = (-2147483648,1105199104)
    

提交回复
热议问题