This Kotlin Operators tutorial explains all major operators used in Kotlin programming, including arithmetic, relational, logical, assignment, unary, and special operators such as in and is. Each operator type is explained with clear syntax, practical examples, and best practices to help beginners and professionals write correct, readable, and efficient Kotlin code.
Kotlin Operators – Complete Tutorial
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
Operators
+Addition-Subtraction*Multiplication/Division%Modulus
Example
val a = 10
val b = 3
println(a + b)
println(a - b)
println(a * b)
println(a / b)
println(a % b)
Best Practices
- Use meaningful variable names for clarity.
- Be careful with integer division; use
Doubleif precision is required.
Relational Operators
Relational operators compare two values and return a Boolean result.
Operators
>Greater than<Less than>=Greater than or equal to<=Less than or equal to==Equal to!=Not equal to
Example
val x = 10
val y = 20
println(x > y)
println(x < y)
println(x == y)
println(x != y)
Best Practices
- Use
==for value comparison, not reference comparison. - Avoid complex comparisons in a single statement.
Logical Operators
Logical operators are used to combine multiple Boolean expressions.
Operators
&&Logical AND||Logical OR!Logical NOT
Example
val isAdult = true
val hasId = false
println(isAdult && hasId)
println(isAdult || hasId)
println(!isAdult)
Best Practices
- Keep Boolean expressions simple and readable.
- Use parentheses for clarity when needed.
Assignment Operators
Assignment operators assign values to variables.
Operators
=Assign+=Add and assign-=Subtract and assign*=Multiply and assign/=Divide and assign%=Modulus and assign
Example
var count = 10
count += 5
count -= 2
count *= 2
count /= 2
Best Practices
- Use compound assignment operators to reduce verbosity.
- Avoid modifying the same variable excessively in one block.
Unary Operators
Unary operators operate on a single operand.
Operators
+Unary plus-Unary minus++Increment--Decrement!Logical NOT
Example
var number = 5
println(+number)
println(-number)
number++
println(number)
number--
println(number)
Best Practices
- Prefer readable expressions over excessive increment or decrement usage.
- Avoid using
++and--inside complex expressions.
in Operator
The in operator checks whether a value exists within a range or collection.
Example with Range
val num = 7
if (num in 1..10) {
println("Number is in range")
}
Example with Collection
val languages = listOf("Kotlin", "Java", "Python")
if ("Kotlin" in languages) {
println("Kotlin is available")
}
Best Practices
- Use
infor clean and readable range checks. - Prefer
inover manual loops.
is Operator
The is operator checks the type of an object at runtime.
Example
fun checkType(value: Any) {
if (value is String) {
println("Length: ${value.length}")
}
}
Best Practices
- Use
isfor safe type checking. - Take advantage of smart casting after
ischecks.
Summary
This chapter covered all essential Kotlin operators, including arithmetic, relational, logical, assignment, unary, and special operators like in and is. Understanding these operators is fundamental for writing logical conditions, loops, and expressions in Kotlin programs.