Scala、Spark 中 == 和 === 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39490236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between == and === in Scala, Spark
提问by Avijit
I am from a Java background and new to Scala.
我来自 Java 背景并且是 Scala 的新手。
I am using Scala and Spark. But I'm not able to understand where I use ==and ===.
我正在使用 Scala 和 Spark。但我无法理解我在哪里使用==和===。
Could anyone let me know in which scenario I need to use these two operators, and what's are difference between ==and ===?
任何人都可以让我知道在这种情况下,我需要使用这两个运营商,以及什么是区别==和===?
回答by Christian Fries
The "==" is using the equalsmethods which checks if the two references point to the same object. The definition of "===" depends on the context/object. For Spark , "===" is using the equalTomethod.
See
" ==" 正在使用equals检查两个引用是否指向同一个对象的方法。" ==="的定义取决于上下文/对象。对于 Spark ,“ ===” 正在使用该equalTo方法。看
- for
==https://spark.apache.org/docs/2.0.0/api/java/org/apache/spark/sql/Column.html#equals(java.lang.Object) - for
===https://spark.apache.org/docs/2.0.0/api/java/org/apache/spark/sql/Column.html#equalTo(java.lang.Object)
- 对于
==https://spark.apache.org/docs/2.0.0/api/java/org/apache/spark/sql/Column.html#equals(java.lang.Object) - 对于
===https://spark.apache.org/docs/2.0.0/api/java/org/apache/spark/sql/Column.html#equalTo(java.lang.Object)
(Since you are referencing Spark:) An important difference for Spark is the return value. For Column:
(因为您引用的是 Spark :)Spark 的一个重要区别是返回值。对于列:
==returns a boolean===returns a column (which contains the result of the comparisons of the elements of two columns)
==返回一个布尔值===返回一列(其中包含两列元素的比较结果)
回答by Lifu Huang
Generally speaking, they are just functions.
一般来说,它们只是函数。
For different types, "==" and "===" might be defined or "overloaded" for different meanings.
对于不同的类型,“==”和“===”可能被定义或“重载”以获得不同的含义。
For example, in some test framework, "===" is defined for some special function. See this.
例如,在某些测试框架中,为某些特殊功能定义了“===”。看到这个。
回答by Aditya Agarwal
ScalaTest lets you use Scala's assertion syntax, but defines a triple equals operator (===) to give you better error messages. The following code would give you an error indicating only that an assertion failed:
ScalaTest 允许您使用 Scala 的断言语法,但定义了一个三等号运算符 (===) 以提供更好的错误消息。下面的代码会给你一个错误,表明断言失败:
assert(1 == 2) Using triple equals instead would give you the more informative error message, "1 did not equal 2":
assert(1 === 2)
assert(1 == 2) 改用三重等于会给您提供更多信息的错误消息,“1 不等于 2”:
断言(1 === 2)
have a look at this page for more details; What is the === (triple-equals) operator in Scala Koans?
查看此页面了解更多详情; Scala Koans 中的 === (triple-equals) 运算符是什么?

