Greetings! Today, we're learning about comparison operators in Scala. This fundamental concept is central to decision-making in coding. As part of our lesson, we will delve into a range of examples involving various data types to comprehend their usage. So, let's dive into the discovery!
Suppose you attend a daily contest in which we gauge who's taller or faster. If we replace these parameters with "greater," "lesser," "equal," or "not equal," we will have the comparison operators! Scala employs several principal ones:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
Let's examine these operators using simple examples:
Scala1@main def run: Unit = 2 val num1 = 5 3 val num2 = 10 4 5 println(num1 == num2) // prints: false, as 5 is not equal to 10 6 println(num1 != num2) // prints: true, as 5 is not equal to 10 7 println(num1 > num2) // prints: false, as 5 is not greater than 10 8 println(num1 < num2) // prints: true, as 5 is less than 10 9 println(num1 >= num2) // prints: false, as 5 is not greater than or equal to 10 10 println(num1 <= num2) // prints: true, as 5 is less than or equal to 10
These comparisons of 5
and 10
using the operators yield boolean values (true or false).
Imagine two siblings: Harry, aged 12, and Ron, aged 14. We can compare their ages using comparison operators:
Scala1@main def run: Unit = 2 val ageHarry = 12 3 val ageRon = 14 4 5 println(ageHarry == ageRon) // prints: false, as Harry and Ron are of different ages 6 println(ageHarry != ageRon) // prints: true, as above 7 println(ageHarry > ageRon) // prints: false, as Harry is younger than Ron 8 println(ageHarry < ageRon) // prints: true, as Harry is younger than Ron 9 println(ageHarry >= ageRon) // prints: false, as Harry is younger or not as old as Ron 10 println(ageHarry <= ageRon) // prints: true, as Harry is younger or as old as Ron (in this case, younger)
Now, let's compare Boolean variables. Please note that we use the operators ==
and !=
in this case:
Scala1@main def run: Unit = 2 val isMorning = true 3 val isAfternoon = false 4 5 println(isMorning == isAfternoon) // prints: false, as true is not the same as false 6 println(isMorning != isAfternoon) // prints: true, as true is not equal to false
In Scala, comparing characters (Char
) works similarly to comparing numbers. The character that comes first alphabetically is "less than" the one that follows. Note, however, that Scala considers capital letters as "less than" lowercase letters because of their arrangement in the Unicode system. For strings (String
), we use the ==
operator, which performs a case-sensitive comparison. Additionally, the operators >
, <
, >=
, and <=
are used to make comparisons in lexicographic order. Let's try comparing the characters 'a' and 'b', as well as the strings "alpha" and "beta":
Scala1@main def run: Unit = 2 val char1 = 'a' 3 val char2 = 'b' 4 5 println(char1 == char2) // prints: false, as 'a' is not the same as 'b' 6 println(char1 != char2) // prints: true, as 'a' and 'b' are different 7 println(char1 > char2) // prints: false, as 'a' comes before 'b' in the alphabet 8 println(char1 < char2) // prints: true, as above 9 println(char1 >= char2) // prints: false, as 'a' is not after or at the same position as 'b' 10 println(char1 <= char2) // prints: true, as 'a' is before or at the same position as 'b' (in this case, before) 11 12 val string1 = "alpha" 13 val string2 = "beta" 14 15 println(string1 == string2) // prints: false, because "alpha" and "beta" are different words 16 println(string1 > string2) // prints: false, because "alpha" comes before "beta" in lexicographic order 17 println(string1 < string2) // prints: true, because "alpha" comes before "beta" in lexicographic order
Note for Java developers: In Scala, the ==
operator performs a value-based comparison for strings, similar to Java's String.equals
method. This means you don't need to use equals
explicitly for checking string equality, reducing the risk of mistakenly checking reference equality as often seen with the ==
operator in Java.
Excellent! Today, you've learned how to apply comparison operators to fundamental data types in Scala using various examples. Stay tuned for hands-on exercises to consolidate the knowledge you've gained today. Let's continue our journey into this intriguing coding universe!