67

I am from a Java background and new to Scala.

I am using Scala and Spark. But I'm not able to understand where I use ==and ===.

Could anyone let me know in which scenario I need to use these two operators, and what's are difference between == and ===?

caffreyd
  • 1,151
  • 1
  • 17
  • 25
Avijit
  • 1,770
  • 5
  • 16
  • 34
  • 3
    Possible duplicate of [Scala equality with type checking?](http://stackoverflow.com/questions/9084464/scala-equality-with-type-checking) – OneCricketeer Sep 14 '16 at 12:18
  • 1
    You should provide a context. `===` and `==` are just functions as any other. They have no special meaning whatsoever. – zero323 Sep 14 '16 at 12:25
  • 1
    @cricket_007 I seriously doubt OP is into Scalaz / Cats. My guess is it is more about Spark SQL. – zero323 Sep 14 '16 at 12:56
  • @zero323 While I agree, I wasn't intending to point at Scalaz, just that "type checking" may be the difference. As you point out, they are just functions and more context needs added as to the objects that are compared – OneCricketeer Sep 14 '16 at 14:36
  • @zero323: I agree, I think the OP is interested in Spark SQL. And may be new enough to Spark to not know that he is working with Datasets/DataFrames. – Josiah Yoder Jul 31 '17 at 15:03

3 Answers3

75

The "==" is using the equals methods which checks if the two references point to the same object. The definition of "===" depends on the context/object. For Spark , "===" is using the equalTo method. See

(Since you are referencing Spark:) An important difference for Spark is the return value. For Column:

  • == returns a boolean

  • === returns a column (which contains the result of the comparisons of the elements of two columns)

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
14

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.

Community
  • 1
  • 1
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
5

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:

assert(1 == 2) Using triple equals instead would give you the more informative error message, "1 did not equal 2":

assert(1 === 2)

have a look at this page for more details; What is the === (triple-equals) operator in Scala Koans?

sumitya
  • 2,631
  • 1
  • 19
  • 32
Aditya Agarwal
  • 693
  • 1
  • 10
  • 17