1. Overview
As we know, two strings are equal if they have the same sequence of characters. In this short tutorial, we’ll see various approaches to comparing Strings in Scala.
2. String Comparison Using the == Method
Let’s see how to compare two Strings using the == method:
val stringOne = "baeldung"
val stringTwo = "baeldung"
assert(stringOne == stringTwo)
Here, the == method checks for the equality of stringOne and stringTwo and returns true.
Alternatively, we can also write it as:
assert(stringOne eq stringTwo)
Comparing strings with the == method has a major advantage – it doesn’t throw a NullPointerException even if a String is null:
val stringOne = "baeldung"
val stringTwo = null
assert(stringOne != stringTwo)
The above code prints a Boolean value false. Internally, the == method checks for null values first and then calls the equals() method of the first String object to check for their equality.
However, *the method == doesn’t compare the Strings in a case-sensitive manner.* So, if we change the value of stringOne to “Baeldung”, it will again print false:
val stringOne = "Baeldung"
val stringTwo = "baeldung"
assert(stringOne != stringTwo)
In such situations, when we want to do a case-insensitive comparison, we can convert both the Strings to upper case or lower case and then compare:
assert(stringOne.toUpperCase() == stringTwo.toUpperCase())
Finally, we should also be aware that calling such methods on a null String throws a NullPointerException.
3. String Comparison Using the equals() Method
We can make use of the equals() method of the String class to check for the equality of two Strings:
assert(stringOne.equals(stringTwo))
However, if the two Strings have a different case, the above assertion will fail.
In such cases, we can use the equalsIgnoreCase() method:
assert(stringOne.equalsIgnoreCase(stringTwo))
Most importantly, we must ensure that the String calling the equals() method is not null. Otherwise, it will throw a NullPointerException.
4. Equality: Scala vs. Java
The concept of equality is defined differently in Java and Scala. Java has essentially two ways to evaluate equality – the == operator that strictly checks for reference equality and the equals() method that, if overridden, checks for the contents of the objects.
Conversely, in Scala, the == method is reserved for comparing any type naturally. This means x == y is true if they refer to the same object. Subsequently, x == y is also true if they hold the same primitive value.
Further, the == is a final method defined in the class Any. It’s written as eq in Scala.
In the case of comparing references in Scala, the == method behaves the same as equals(). But, we can always change the behavior of equals() by overriding it in the classes we define. However, we cannot override == as it’s a final method.
5. Conclusion
In this article, we discussed how to compare two Strings using the == and equals() methods. We also explored how to do case-insensitive comparisons.
As always, the full source code for the examples can be found over on GitHub.