Difference between == and === in Scala, Spark

ScalaApache Spark

Scala Problem Overview


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 ===?

Scala Solutions


Solution 1 - Scala

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)

Solution 2 - Scala

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.

Solution 3 - Scala

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; https://stackoverflow.com/questions/10489548/what-is-the-triple-equals-operator-in-scala-koans

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAvijitView Question on Stackoverflow
Solution 1 - ScalaChristian FriesView Answer on Stackoverflow
Solution 2 - ScalaLifu HuangView Answer on Stackoverflow
Solution 3 - ScalaAditya AgarwalView Answer on Stackoverflow