➕ Kotlin – Operators: Arithmetic, Logical, Relational & More
🧲 Introduction – Why Learn Kotlin Operators?
Operators are symbols that perform computations, comparisons, and logic operations on variables and values. Kotlin supports a wide range of operators similar to Java but with added clarity and null-safety. Mastering Kotlin operators allows you to write efficient, readable, and expressive code.
🎯 In this guide, you’ll learn:
- All major categories of Kotlin operators
- Syntax and examples for arithmetic, logical, and relational operations
- How Kotlin handles null-safe operations and operator overloading
- Best practices for operator usage
🧮 Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Name | Example | Output | 
|---|---|---|---|
| + | Addition | 10 + 5 | 15 | 
| - | Subtraction | 10 - 5 | 5 | 
| * | Multiplication | 10 * 5 | 50 | 
| / | Division | 10 / 2 | 5 | 
| % | Modulus | 10 % 3 | 1 | 
✨ Example:
val a = 12
val b = 4
println("Sum: ${a + b}, Product: ${a * b}")
✅ Output:
Sum: 16, Product: 48
⚖️ Relational (Comparison) Operators
Used to compare values. They return true or false.
| Operator | Description | Example | Output | 
|---|---|---|---|
| == | Equal to | 5 == 5 | true | 
| != | Not equal to | 5 != 3 | true | 
| > | Greater than | 6 > 2 | true | 
| < | Less than | 2 < 5 | true | 
| >= | Greater than or equal | 3 >= 3 | true | 
| <= | Less than or equal | 2 <= 1 | false | 
🔁 Assignment Operators
Used to assign and update values in variables.
| Operator | Description | Example | Equivalent To | 
|---|---|---|---|
| = | Assignment | a = 10 | – | 
| += | Add and assign | a += 5 | a = a + 5 | 
| -= | Subtract and assign | a -= 3 | a = a - 3 | 
| *= | Multiply and assign | a *= 2 | a = a * 2 | 
| /= | Divide and assign | a /= 2 | a = a / 2 | 
| %= | Modulo and assign | a %= 3 | a = a % 3 | 
⚙️ Unary Operators
Operate on a single operand.
| Operator | Meaning | Example | 
|---|---|---|
| + | Unary plus | +a | 
| - | Unary minus | -a | 
| ++ | Increment | a++ | 
| -- | Decrement | a-- | 
| ! | Logical NOT | !true | 
🤔 Logical Operators
Used to build compound boolean expressions.
| Operator | Description | Example | Result | 
|---|---|---|---|
| && | Logical AND | true && false | false | 
| ` | ` | Logical OR | |
| ! | Logical NOT | !true | false | 
val age = 25
val isMember = true
if (age > 18 && isMember) {
    println("Eligible")
}
✅ Output:
Eligible
🔎 Kotlin Identity & Equality Operators
| Operator | Description | Example | 
|---|---|---|
| == | Structural equality | a == b(content match) | 
| === | Referential equality | a === b(same object) | 
val str1 = "Hello"
val str2 = "Hello"
println(str1 == str2)   // true
println(str1 === str2)  // might be true (same reference in memory)
❔ Null-Safety Operators
Kotlin introduces special operators for handling nulls safely.
| Operator | Name | Example | Purpose | 
|---|---|---|---|
| ?. | Safe call | name?.length | Avoids NullPointerException | 
| ?: | Elvis operator | name ?: "Guest" | Default value if null | 
| !! | Not-null assert | name!! | Throws NPE if null | 
💡 Operator Overloading in Kotlin
Kotlin allows custom operator overloading for classes using predefined keywords.
✅ Example:
data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point) = Point(x + other.x, y + other.y)
}
val p1 = Point(2, 3)
val p2 = Point(4, 1)
println(p1 + p2) // Output: Point(x=6, y=4)
📌 Summary – Recap & Next Steps
Kotlin offers a rich set of operators that make arithmetic, logic, and comparison tasks intuitive and expressive. It also introduces null-safe operators and operator overloading, making Kotlin highly flexible and safe.
🔍 Key Takeaways:
- Use arithmetic, logical, and relational operators like in Java.
- Kotlin supports structural (==) and referential (===) equality.
- Use null-safety operators to avoid NullPointerExceptions.
- Operator overloading enables expressive custom logic in classes.
⚙️ Practical Use:
Operators are critical in every Kotlin application—from simple calculations to complex conditions in Android, backend services, and business logic.
❓ FAQs – Kotlin Operators
❓ What’s the difference between == and === in Kotlin?
✅ == checks value equality, while === checks reference equality (if two variables point to the same object).
❓ How does Kotlin handle nulls with operators?
✅ Use the safe call operator ?., the Elvis operator ?:, or !! for asserting non-null.
❓ Can I overload operators in Kotlin?
✅ Yes. Kotlin allows operator overloading by defining functions with the operator keyword in classes.
❓ Does Kotlin support all Java-style operators?
✅ Yes, including arithmetic, comparison, assignment, and logical operators—plus Kotlin adds null-safety operators.
❓ Can I use ++ and -- in Kotlin like in Java?
✅ Yes. Kotlin supports a++, --b, etc., for increment/decrement operations.
Share Now :
