一.简介
根据官方API的定义:
- final def ==(arg0: Any): Boolean
The expression x == that is equivalent to if (x eq null) that eq null else x.equals(that). - final def eq(arg0: AnyRef): Boolean
Tests whether the argument (that) is a reference to the receiver object (this). - def equals(arg0: Any): Boolean
The equality method for reference types.
二.代码测试
package big.data.analyse.scala.eq_equals
/**
* Created by zhen on 2019/4/9.
*/
case class Bread(brand:String, price:Int)
object EqEquals {
def main(args: Array[String]) {
/**
* 基本类型
*/
val j1 = "234"
val j2 = 234.toString
println(j1 eq j2) // 引用
println(j1 equals j2) // 值
println(j1 == j2) // 当比较的对象为null时等价eq,否则等价equals
println("---------------------------")
/**
* 集合类型
*/
val a1 = Array("x", "y")
val a2 = Array("x", "y")
println(a1 equals a2)
println(a1 eq a2)
println(a1 == a2)
println(a1 sameElements a2)
val m1 = Map(1->"x", 2->"y")
val m2 = Map(1->"x", 2->"y")
println(m1 sameElements m2)
val m3 = Map(1->"x", 2->"z")
println(m1 sameElements m3)
println("===========================")
/**
* 引用类型
*/
val b1 = Bread("BreadTalk", 50)
val b2 = Bread("BreadTalk", 50)
println(b1 eq b2) // 引用
println(b1 equals b2) // 值
println(b1 == b2) // 当比较的对象为null时等价eq,否则等价equals
}
}
三.结果