Comparison operators
- Kotlin course
- Beginner
- Lesson 26/116
- 4 min
Learn to compare values with ==, !=, <, >, <= and >=.
Comparison operators
Comparison operators compare two values and return a Boolean: == (equal), != (not equal), < (less than), > (greater than), <= (less or equal), >= (greater or equal).
These operators are commonly used in if conditions and loop conditions.
println(5 == 5) // true
println(3 < 2) // false
println(10 >= 10) // truePractice Try comparison
Write a fun main(). Declare val a = 7 and val b = 10. Compare with println(a < b).
Sample solution
fun main() {
val a = 7
val b = 10
println(a < b)
}Expected output
true
== vs ===
In Kotlin == checks structural equality (is the content the same). For reference equality (is it the same object) use ===.
For simple types (Int, String) == is enough; it does a content comparison. For objects, == actually calls the equals() method. So you can safely use == to check whether two separate lists hold the same content.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Comparison operators: match the terms
| Term | Meaning |
|---|---|
== |
Equal |
!= |
Not equal |
< |
Less than |
>= |
Greater or equal |
2 What does `3 < 2` return?
- true
- false Correct answer
- 3
- It throws an error
Explanation: 3 is not less than 2, so it returns false.
3 Comparison operators: fill in the missing part
val same = (5 ____ 5)
println(same) // true
val same = (5 == 5)
println(same) // true
Explanation: Use == for equality.