Comparison Operators in Kotlin Hindi
Here's a table of equality and comparison operators, their meaning, and corresponding functions:
Operator | Meaning | Expression | Translates to |
---|---|---|---|
> | greater than | a > b | a.compareTo(b) > 0 |
< | less than | a < b | a.compareTo(b) < 0 |
>= | greater than or equals to | a >= b | a.compareTo(b) >= 0 |
<= | less than or equals to | a < = b | a.compareTo(b) <= 0 |
== | is equal to | a == b | a?.equals(b) ?: (b === null) |
!= | not equal to | a != b | !(a?.equals(b) ?: (b === null)) |
Comparison and equality operators are used in control flow such as if expression, when expression, and loops.
Example: Comparison and Equality Operators
fun main(args: Array<String>) {
val a = -12
val b = 12
// use of greater than operator
val max = if (a > b) {
println("a is larger than b.")
a
} else {
println("b is larger than a.")
b
}
println("max = $max")
}
When you run the program, the output will be:
b is larger than a. max = 12