scala> 1 == 2
res31: Boolean = false
scala> 1 != 2
res32: Boolean = true
scala> 2 == 2
res33: Boolean = true
scala> List(1, 2, 3) == List(1, 2, 3)
res34: Boolean = true
scala> List(1, 2, 3) == List(4, 5, 6)
res35: Boolean = false
scala> 1 == 1.0
res36: Boolean = true
scala> List(1, 2, 3) == "hello"
res37: Boolean = false
scala> List(1, 2, 3) == null
res38: Boolean = false
scala> null == List(1, 2, 3)
res39: Boolean = false
This kind of comarison will yield true on different objects, so long as their contents are the same and their equals method is written to be based on contents.
scala> ("he"+"llo") == "hello"
res40: Boolean = true
In Java, you can use == to compare both primitive and reference types.
On primitive types, Java's == compares value equality, as in Scala.
On reference types, however, Java's == compares reference equality, which means the two variables point to the same object on the JVM's heap.
Scala provides a facility for comparing reference equality, as well, under the name eq. However, eq and its opposite, ne, only apply to objects that directly map to Java objects.
댓글 없음:
댓글 쓰기