Kotlin Operators
Operators are symbols that perform operations on values: arithmetic, comparison, logic and more. Kotlin's operators will look familiar if you know any C-style language, with a few useful additions.
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3 (Int division) |
% |
remainder | 7 % 2 |
1 |
++ |
increment | x++ |
adds 1 |
-- |
decrement | x-- |
subtracts 1 |
fun main() {
val boxes = 17
val perShelf = 5
println("Full shelves: ${boxes / perShelf}")
println("Left over: ${boxes % perShelf}")
var counter = 0
counter++
counter++
counter--
println("Counter: $counter")
}Full shelves: 3
Left over: 2
Counter: 1
Assignment operators
| Operator | Same as |
|---|---|
x = 5 |
assign |
x += 3 |
x = x + 3 |
x -= 3 |
x = x - 3 |
x *= 3 |
x = x * 3 |
x /= 3 |
x = x / 3 |
x %= 3 |
x = x % 3 |
fun main() {
var balance = 100
balance += 50
balance -= 30
balance *= 2
println(balance)
}240
Comparison operators
Comparisons produce a Boolean.
| Operator | Meaning |
|---|---|
== / != |
equal / not equal (compares values, calls equals()) |
> < >= <= |
greater, less, greater-or-equal, less-or-equal |
=== / !== |
same object / different object (reference equality) |
fun main() {
val a = "kot" + "lin"
val b = "kotlin"
println(a == b)
println(5 >= 5)
println(3 != 3)
println("apple" < "banana")
}true
true
false
true
Note: In Java,
==on strings compares references, a famous trap. In Kotlin==compares contents, so it does what you expect. Use===only when you really need to know whether two variables point to the same object.
Logical operators
| Operator | Name | True when |
|---|---|---|
&& |
and | both sides are true |
|| |
or | at least one side is true |
! |
not | the value is false |
&& and || short-circuit: the right side isn't evaluated if the left side already decides the answer.
fun main() {
val age = 20
val hasTicket = true
println(age >= 18 && hasTicket)
println(age < 12 || age > 65)
println(!hasTicket)
}true
false
false
Kotlin-specific operators
| Operator | Purpose | Chapter |
|---|---|---|
.. / ..< |
ranges: 1..5, 0..<n |
Ranges |
in / !in |
membership: x in 1..10 |
Ranges, Collections |
?. |
safe call on nullable values | Null Safety |
?: |
Elvis: default for null |
Null Safety |
!! |
non-null assertion | Null Safety |
is / !is |
type check | Classes |
as |
type cast | Inheritance |
fun main() {
val score = 72
println(score in 50..100)
println('z' !in "kotlin")
}true
true
Operator precedence
Multiplication and division bind tighter than addition and subtraction; comparison comes after arithmetic, then &&, then ||. When in doubt, use parentheses — they cost nothing and make intent clear: (a + b) * c.
Tip: Behind the scenes operators are function calls:
a + bisa.plus(b). That's why you can define+for your own classes withoperator fun plus(...).
Test yourself
What is 17 % 5?
- 3
- 2
- 3.4
- 0
In Kotlin, what does == compare for strings?
- Memory addresses
- Contents (structural equality)
- Lengths
- Hash codes only
What does x += 4 mean?
- x = 4
- x = x + 4
- x == x + 4
- x = 4 + 4