==
or !=
to compare String
s SC-R1026Because comparison operators in languages like Java are usually used for reference equality checks, types such as String
implement a method called .equals
that allows you to check if two String
s are equal or not. However, Scala allows the user to perform value equality checks using the comparison operators as long as the object's class overrides the equals
method. It is suggested that you use comparison operators instead of the .equals
method when comparing 2 strings.
if (str1.equals(str2)) {
// ...
}
if (str1 == str2) {
// ...
}