scalaequalsequalityscala-java-interop

What's the difference between == and .equals in Scala?


What is the difference between == and .equals() in Scala, and when to use which?

Is the implementation the same as in Java?

There is a similar question (Why == operator and equals() behave differently for values of AnyVal in Scala), but that is specific to AnyVal. This question is concerned with the more general case of Any.


Solution

  • You normally use ==, it routes to equals, except that it treats nulls properly. Reference equality (rarely used) is eq.