Kotlin Operators โ Arithmetic, Comparison, and Logical
Operators are symbols that perform operations on values. Kotlin supports arithmetic, comparison, logical, bitwise, and assignment operators.
What You Will Learn
- Arithmetic operators: +, -, *, /, %
- Comparison operators: ==, !=, <, >, <=, >=
- Logical operators: &&, ||, !
- Assignment operators: +=, -=, *=, /=
- Increment and decrement: ++, --
Arithmetic Operators
Arithmetic
fun main() {
val a = 10
val b = 3
println(a + b) // 13
println(a - b) // 7
println(a * b) // 30
println(a / b) // 3 (integer division)
println(a % b) // 1 (remainder)
println(10.0 / 3) // 3.333...
}Integer division truncates the result. 10 / 3 = 3, not 3.33. Use Double for decimal division.
Comparison Operators
Comparison
fun main() {
val x = 10
val y = 20
println(x == y) // equal
println(x != y) // not equal
println(x < y) // less than
println(x > y) // greater than
println(x <= 10) // less than or equal
println(x >= 10) // greater than or equal
}== compares values (structural equality). In Kotlin, == is equivalent to .equals() for objects.
Logical Operators
Logical Operators
fun main() {
val a = true
val b = false
println(a && b) // AND
println(a || b) // OR
println(!a) // NOT
val age = 25
val hasId = true
println(age >= 18 && hasId) // both must be true
}&& requires both sides true. || requires at least one side true. ! inverts a boolean. Kotlin uses short-circuit evaluation โ the second operand is only evaluated if needed.
Assignment and Increment
Assignment Operators
fun main() {
var n = 10
n += 5; println(n) // 15
n -= 3; println(n) // 12
n *= 2; println(n) // 24
n /= 4; println(n) // 6
n++; println(n) // 7
n--; println(n) // 6
}+= adds and assigns. *= multiplies and assigns. n++ increments by 1. n-- decrements by 1.
Practice Exercise
What prints? val x = 7 println(x % 3) println(x > 5 && x < 10)
Quick Quiz
What is the result of 10 / 3 in Kotlin when both operands are Int?
Frequently Asked Questions
Related Tutorials
Written by KotlinGuide Editorial Team ยท Reviewed by KotlinGuide Technical Review