WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Scala · Basic · question 19 of 100

What are the differences between == and equals in Scala?

📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

In Scala, ‘==‘ and ‘equals‘ are used to compare objects, but they have some differences in terms of their behavior and implementation.

1. ‘==‘ (Equality operator):

The ‘==‘ operator in Scala is used to compare if two objects are equal in terms of their values. It is a method defined in ‘AnyRef‘ class, which is the base class for all reference classes in Scala. When you use ‘==‘, it internally calls the ‘equals‘ method, and it also handles the null values gracefully.

Here’s an example:

val str1 = "Scala"
val str2 = "Scala"
val str3 = new String("Scala")

println(str1 == str2) // true
println(str1 == str3) // true

In this example, ‘str1 == str2‘ and ‘str1 == str3‘ return ‘true‘ because the values they refer to are the same.

2. ‘equals‘ (Equals method):

The ‘equals‘ method is used to determine the equality of objects in terms of their values. It is also defined in the ‘AnyRef‘ class, and it is designed to be overridden by subclasses to provide custom behavior for value-based comparisons.

However, by default, the ‘equals‘ method in the ‘AnyRef‘ class uses reference (identity) comparison, meaning it will only return ‘true‘ if both objects point to the same object in memory.

Here’s an example:

val str1 = "Scala"
val str2 = "Scala"
val str3 = new String("Scala")

println(str1.equals(str2)) // true
println(str1.equals(str3)) // true

In this example, ‘str1.equals(str2)‘ and ‘str1.equals(str3)‘ return ‘true‘ because the ‘String‘ class overrides the ‘equals‘ method to compare the values of the strings, and the values are the same.

However, if we use a custom class without overriding equals, the default reference comparison will be used:

case class Person(name: String, age: Int)

val p1 = Person("Alice", 30)
val p2 = Person("Alice", 30)
val p3 = p1

println(p1.equals(p2)) // false
println(p1.equals(p3)) // true

In this case, although ‘p1‘ and ‘p2‘ have the same data, they are different objects in memory, so ‘p1.equals(p2)‘ returns ‘false‘. But ‘p1‘ and ‘p3‘ are the same object in memory, so ‘p1.equals(p3)‘ returns ‘true‘.

In summary, the main differences between ‘==‘ and ‘equals‘ in Scala are:

- The ‘==‘ operator is a method that internally calls the ‘equals‘ method and handles null values. It is used for value-based comparisons.

- The ‘equals‘ method is designed to be overridden by subclasses to provide custom value-based comparison behavior. By default, it uses reference (identity) comparison.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Scala interview — then scores it.
📞 Practice Scala — free 15 min
📕 Buy this interview preparation book: 100 Scala questions & answers — PDF + EPUB for $5

All 100 Scala questions · All topics