Greetings! Today, we're studying comparison operators in Kotlin, which are vital for decision-making in coding. Through engaging examples with different data types, we'll grasp the power of these operators. Let's begin our exploration!
Imagine a daily competitive event where we judge who is taller or faster. Replace these with "greater," "lesser," "equal," or "not equal," and you've got comparison operators! Kotlin uses six basic ones:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
Let's explore these through simple examples:
Kotlin1val num1 = 5 2val num2 = 10 3 4println(num1 == num2) //prints: false, as 5 is not equal to 10 5println(num1 != num2) //prints: true, as 5 is not equal to 10 6println(num1 > num2) //prints: false, as 5 is not greater than 10 7println(num1 < num2) //prints: true, as 5 is less than 10 8println(num1 >= num2) //prints: false, as 5 is not greater than or equal to 10 9println(num1 <= num2) //prints: true, as 5 is less than or equal to 10
Comparing 5
and 10
using these operators produces boolean values (true or false).
Consider two brothers: Harry, who is 12, and Ron, who is 14. We can use age comparison operators to compare their ages:
Kotlin1val ageHarry = 12 2val ageRon = 14 3 4println(ageHarry == ageRon) // prints: false, because Harry and Ron are of different ages 5println(ageHarry != ageRon) // prints: true, as above 6println(ageHarry > ageRon) // prints: false, because Harry is not older than Ron 7println(ageHarry < ageRon) // prints: true, because Harry is younger than Ron 8println(ageHarry >= ageRon) // prints: false, as Harry is not older or as old as Ron 9println(ageHarry <= ageRon) // prints: true, as Harry is younger or as old as Ron (in this case, younger)
Now, let's compare Boolean variables. Note that ==
and !=
are the operators we use here:
Kotlin1val isMorning = true 2val isAfternoon = false 3 4println(isMorning == isAfternoon) // prints: false, as true does not equal false 5println(isMorning != isAfternoon) // prints: true, as true is not equal to false
In Kotlin, when comparing characters (Char
), the character that comes first alphabetically is considered "less than" the one that follows. However, it's important to note that capital letters are considered "less than" lowercase letters because of their order in the Unicode system. For strings (String
), the comparison is made letter by letter from the beginning, determining which string comes first based on alphabetical order, taking into account the capitalization. Thus, a string with a capital letter might come before a similar string with its first letter in lowercase, just like organizing words in a dictionary where case affects placement. Let's compare the characters 'a'
and 'b'
, and the strings "alpha"
and "beta"
using comparison operators:
Kotlin1val char1 = 'a' 2val char2 = 'b' 3 4println(char1 == char2) // prints: false, because 'a' is not the same as 'b' 5println(char1 != char2) // prints: true, as 'a' and 'b' are different 6println(char1 > char2) // prints: false, as 'a' comes before 'b' in the alphabet 7println(char1 < char2) // prints: true, as above 8println(char1 >= char2) // prints: false, as 'a' is not after or in the same position as 'b' 9println(char1 <= char2) // prints: true, as 'a' is before or in the same position as 'b' (in this case, before) 10 11val string1 = "alpha" 12val string2 = "beta" 13 14println(string1 == string2) //prints: false, because "alpha" and "beta" are different words 15println(string1 != string2) //prints: true, as above 16println(string1 > string2) //prints: false, because "a" comes before "b" in the dictionary 17println(string1 < string2) //prints: true, because "a" comes before "b" in the dictionary 18println(string1 >= string2) //prints: false, because "a" comes before "b" in the dictionary 19println(string1 <= string2) //prints: true, because "a" comes before "b" in the dictionary
Remember, string comparisons are case-sensitive!
Great work! Today, you learned how to apply Kotlin comparison operators to basic data types using a variety of examples. Stay tuned for hands-on exercises to solidify your knowledge. Let's continue exploring this exciting world of coding!