How to Use Comparison Operators in Kotlin: Syntax and Examples
In Kotlin, you use
== to check if two values are equal and != to check if they are not equal. Other comparison operators like <, <=, >, and >= compare values to see if one is less than, less or equal, greater than, or greater or equal to another.Syntax
Kotlin provides simple comparison operators to compare values. Here are the main ones:
==: Checks if two values are equal.!=: Checks if two values are not equal.<: Checks if the left value is less than the right value.<=: Checks if the left value is less than or equal to the right value.>: Checks if the left value is greater than the right value.>=: Checks if the left value is greater than or equal to the right value.
These operators return a Boolean value: true or false.
kotlin
val a = 5 val b = 10 val isEqual = (a == b) // false val isNotEqual = (a != b) // true val isLess = (a < b) // true val isLessOrEqual = (a <= b) // true val isGreater = (a > b) // false val isGreaterOrEqual = (a >= b) // false
Example
This example shows how to use comparison operators in Kotlin to compare two numbers and print the results.
kotlin
fun main() {
val x = 7
val y = 12
println("x == y: ${x == y}")
println("x != y: ${x != y}")
println("x < y: ${x < y}")
println("x <= y: ${x <= y}")
println("x > y: ${x > y}")
println("x >= y: ${x >= y}")
}Output
x == y: false
x != y: true
x < y: true
x <= y: true
x > y: false
x >= y: false
Common Pitfalls
One common mistake is using = instead of == for comparison. In Kotlin, = is for assignment, not comparison.
Another pitfall is confusing == with ===. The == operator checks structural equality (if values are equal), while === checks referential equality (if two variables point to the same object).
kotlin
fun main() {
val a = 5
val b = 5
// Wrong: assignment instead of comparison
// if (a = b) { // This will cause a compile error
// println("Equal")
// }
// Correct: use == for comparison
if (a == b) {
println("a and b are equal")
}
// Referential equality example
val str1 = "hello"
val str2 = "hello"
println(str1 == str2) // true, values are equal
println(str1 === str2) // true or false depending on JVM optimizations
}Output
a and b are equal
true
true
Quick Reference
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Checks if two values are equal | 5 == 5 | true |
| != | Checks if two values are not equal | 5 != 3 | true |
| < | Checks if left is less than right | 3 < 5 | true |
| <= | Checks if left is less or equal to right | 5 <= 5 | true |
| > | Checks if left is greater than right | 7 > 2 | true |
| >= | Checks if left is greater or equal to right | 7 >= 7 | true |
Key Takeaways
Use == and != to check if values are equal or not equal in Kotlin.
Use <, <=, >, >= to compare numeric or comparable values.
Remember = is for assignment, not comparison; use == for checking equality.
== checks if values are equal, === checks if two references point to the same object.
Comparison operators return Boolean values: true or false.