Kotlin Operators
Perform operations and calculations in Kotlin
⚡ What are Kotlin Operators?
Operators are symbols that perform operations on variables and values. They include arithmetic (+, -), comparison (==, !=), and logical (&&, ||) operators for calculations and decisions.
// Basic operators in action
val a = 10
val b = 5
val sum = a + b // Addition: 15
val isEqual = a == b // Comparison: false
val bothTrue = true && false // Logical: false
Output:
sum: 15
isEqual: false
bothTrue: false
Types of Operators
Arithmetic
Math operations
val result = 10 + 5
Comparison
Compare values
val isGreater = 10 > 5
Logical
Combine conditions
val both = true && false
Assignment
Assign values
var x = 10; x += 5
🔹 Arithmetic Operators
Perform mathematical calculations:
val a = 20
val b = 6
// Basic arithmetic
val addition = a + b // 26
val subtraction = a - b // 14
val multiplication = a * b // 120
val division = a / b // 3 (integer division)
val remainder = a % b // 2 (modulus)
// Floating point division
val preciseDiv = 20.0 / 6 // 3.3333333333333335
// Increment and decrement
var counter = 5
counter++ // counter becomes 6
counter-- // counter becomes 5
// Pre and post increment
var x = 5
val y = ++x // x = 6, y = 6 (pre-increment)
val z = x++ // x = 7, z = 6 (post-increment)
Output:
addition: 26
subtraction: 14
multiplication: 120
division: 3
remainder: 2
preciseDiv: 3.3333333333333335
🔹 Comparison Operators
Compare values and return true or false:
val score1 = 85
val score2 = 92
val score3 = 85
// Equality operators
val isEqual = score1 == score3 // true
val isNotEqual = score1 != score2 // true
// Relational operators
val isGreater = score2 > score1 // true
val isLess = score1 < score2 // true
val isGreaterEqual = score1 >= score3 // true
val isLessEqual = score1 <= score2 // true
// Comparing strings
val name1 = "Alice"
val name2 = "Bob"
val name3 = "Alice"
val sameNames = name1 == name3 // true
val differentNames = name1 != name2 // true
val alphabetical = name1 < name2 // true (A comes before B)
Output:
isEqual: true
isNotEqual: true
isGreater: true
sameNames: true
alphabetical: true
🔹 Logical Operators
Combine multiple conditions:
val age = 25
val hasLicense = true
val hasInsurance = false
// AND operator (&&) - both conditions must be true
val canDrive = age >= 18 && hasLicense // true
val canDriveLegally = canDrive && hasInsurance // false
// OR operator (||) - at least one condition must be true
val isAdult = age >= 18 || age >= 21 // true
val hasDocuments = hasLicense || hasInsurance // true
// NOT operator (!) - reverses the boolean value
val isMinor = !isAdult // false
val needsLicense = !hasLicense // false
// Complex conditions
val canRentCar = (age >= 25) && hasLicense && hasInsurance // false
val needsDocuments = !hasLicense || !hasInsurance // true
Output:
canDrive: true
canDriveLegally: false
isAdult: true
hasDocuments: true
isMinor: false
canRentCar: false
🔹 Assignment Operators
Assign and modify variable values:
// Basic assignment
var points = 100
// Compound assignment operators
points += 50 // points = points + 50 → 150
points -= 20 // points = points - 20 → 130
points *= 2 // points = points * 2 → 260
points /= 4 // points = points / 4 → 65
points %= 10 // points = points % 10 → 5
// String concatenation assignment
var message = "Hello"
message += " World" // message = "Hello World"
message += "!" // message = "Hello World!"
// Multiple assignments
var a = 10
var b = 20
var c = 30
// Swap values
val temp = a
a = b // a = 20
b = temp // b = 10
Output:
Final points: 5
message: "Hello World!"
After swap - a: 20, b: 10
🔹 Operator Precedence
Understanding the order of operations:
Order (highest to lowest):
- Parentheses: ( )
- Unary: ++, --, !, -
- Multiplicative: *, /, %
- Additive: +, -
- Comparison: <, >, <=, >=
- Equality: ==, !=
- Logical AND: &&
- Logical OR: ||
- Assignment: =, +=, -=, etc.
// Without parentheses
val result1 = 10 + 5 * 2 // 20 (5 * 2 first, then + 10)
// With parentheses
val result2 = (10 + 5) * 2 // 30 (10 + 5 first, then * 2)
// Complex expression
val complex = 10 + 5 * 2 > 15 && true // true
// Breakdown: 5 * 2 = 10, 10 + 10 = 20, 20 > 15 = true, true && true = true