一、对象相等性
### --- Java 中可以 == 来比较基本类型和引用类型:
~~~ 对基本类型而言,比较的是值的相等性
~~~ 对引用类型而言,比较的是引用相等性,即两个变量是否指向JVM堆上的同个对象
### --- Scala中,要比较两个基础类型的对象是否相等,可以使用 == 或 !=;
1 == 1
1 != 2
2 == 2
~~~ # == 或 != 可以比较同一类型的两个对象;
List(1,2,3) == List(1,2,3)
List(1,2,3) != Array(4,5,6)
~~~ # 或 != 还可以比较不同类型的两个对象:
2 == 2.0
List(1,2,3) == "Scala"
### --- 编程代码
package yanqi.cn.part01
object ObjectCompareDemo {
def main(args: Array[String]): Unit = {
println(1==1)
println(1!=2)
println(1==2)
val flag=List(1,2,3)==List(4,5,6)
println(flag)
println(List(1,2,3)!=Array(1,2,3))
//比较不同类型的对象
println(2==2.0)
println(List(1,2,3)=="Scala")
}
}
### --- 编译打印
D:\JAVA\jdk1.8.0_231\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=52219:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.10000——javaproject\NO.Z.00002.Hadoop\ScalaPro\out\production\ScalaPro;D:\JAVA\scala-2.12.2\lib\scala-library.jar;D:\JAVA\scala-2.12.2\lib\scala-reflect.jar yanqi.cn.part01.ObjectCompareDemo
true
true
false
false
true
true
false
Process finished with exit code 0