Lesson 4
Exploring Comparison Operators in Scala
Introduction and Lesson Overview

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!

Understanding Comparison Operators

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:

  1. Equal to (==)
  2. Not equal to (!=)
  3. Greater than (>)
  4. Less than (<)
  5. Greater than or equal to (>=)
  6. Less than or equal to (<=)

Let's examine these operators using simple examples:

Scala
1@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).

Applying Comparison Operators on Numbers

Imagine two siblings: Harry, aged 12, and Ron, aged 14. We can compare their ages using comparison operators:

Scala
1@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)
Comparison Operators with Booleans

Now, let's compare Boolean variables. Please note that we use the operators == and != in this case:

Scala
1@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
Comparing Characters and Strings

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":

Scala
1@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.

Lesson Summary and Upcoming Practice

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.