Kotlin Operators

An operator is a symbol that performs an action on one or more values. Think of operators as the verbs of programming — they do things to data.

Arithmetic Operators

These perform math operations, just like a calculator.

val a = 20
val b = 6

println(a + b)   // 26  — Addition
println(a - b)   // 14  — Subtraction
println(a * b)   // 120 — Multiplication
println(a / b)   // 3   — Division (integer, drops remainder)
println(a % b)   // 2   — Modulus (remainder after division)

Integer Division vs Decimal Division

val x = 7 / 2          // Result: 3  (Int ÷ Int → Int, decimal dropped)
val y = 7.0 / 2        // Result: 3.5 (Double ÷ Int → Double)
val z = 7 / 2.0        // Result: 3.5

Comparison Operators

Comparison operators check a relationship between two values and return true or false.

Operator | Meaning              | Example        | Result
---------|----------------------|----------------|--------
==       | Equal to             | 5 == 5         | true
!=       | Not equal to         | 5 != 3         | true
>        | Greater than         | 10 > 7         | true
<        | Less than            | 3 < 1          | false
>=       | Greater or equal     | 5 >= 5         | true
<=       | Less or equal        | 4 <= 6         | true
val userAge = 18
val canVote = userAge >= 18
println(canVote)   // true

Logical Operators

Logical operators combine multiple conditions. Think of them as the words "and", "or", and "not".

Operator | Meaning                        | Example
---------|--------------------------------|--------------------------------
&&       | AND — both must be true        | isLoggedIn && hasPremium
||       | OR  — at least one must be true| isGuest || hasFreeTrial
!        | NOT — flips true to false      | !isBlocked

Diagram — Logical AND

isLoggedIn = true
hasPremium = false

isLoggedIn && hasPremium
= true && false
= false   ← Both must be true. One is false, so result is false.

Diagram — Logical OR

isGuest = false
hasFreeTrial = true

isGuest || hasFreeTrial
= false || true
= true    ← At least one is true, so result is true.

Assignment Operators

The basic assignment operator (=) stores a value in a variable. Combined assignment operators update a variable using itself.

var points = 10

points += 5    // points = points + 5  →  15
points -= 3    // points = points - 3  →  12
points *= 2    // points = points * 2  →  24
points /= 4    // points = points / 4  →  6
points %= 4    // points = points % 4  →  2

Increment and Decrement Operators

var count = 5

count++   // count becomes 6 — post-increment
count--   // count becomes 5 — post-decrement
++count   // count becomes 6 — pre-increment
--count   // count becomes 5 — pre-decrement

Pre vs Post — What Is the Difference

var x = 5
println(x++)   // prints 5, THEN increments x to 6
println(++x)   // increments x to 7 FIRST, THEN prints 7

Range Operators

Kotlin has a unique range operator (..) that creates a sequence of values between two endpoints.

val range = 1..5       // Creates: 1, 2, 3, 4, 5 (inclusive)
val charRange = 'a'..'e'  // Creates: a, b, c, d, e

// Check if a value is in the range
val score = 75
println(score in 50..100)   // true
println(score !in 80..100)  // true

Diagram — Range Visualized

1..10
|---|---|---|---|---|---|---|---|---|---|
1   2   3   4   5   6   7   8   9  10

until 1..10 (excludes 10):
|---|---|---|---|---|---|---|---|---|
1   2   3   4   5   6   7   8   9

Elvis Operator

The Elvis operator (?:) is a Kotlin-specific operator. It provides a default value when the left side is null. The name comes from the fact that ?: looks like Elvis Presley's hairstyle turned sideways.

val userName: String? = null
val displayName = userName ?: "Guest"
println(displayName)   // Guest

val inputAge: Int? = null
val age = inputAge ?: 18
println(age)   // 18

Operator Precedence

When multiple operators appear in one expression, Kotlin evaluates them in a specific order — similar to the math rule "multiply before you add."

High → Low Precedence:
1. Unary:    ++ -- ! (applied to one value)
2. Multiply: * / %
3. Add:      + -
4. Range:    ..
5. Compare:  > < >= <=
6. Equality: == !=
7. And:      &&
8. Or:       ||
9. Assign:   = += -= ...

Example:
val result = 2 + 3 * 4   // = 2 + 12 = 14  (not 20)
val result2 = (2 + 3) * 4 // = 5 * 4 = 20  (parentheses change order)

Use parentheses to make the intended order clear whenever an expression could be ambiguous.

Leave a Comment

Your email address will not be published. Required fields are marked *