Kotlin Operators

Kotlin Operators – Complete Beginner Guide With Examples
Operators are one of the most fundamental concepts in any programming language. In Kotlin, operators are special symbols used to perform operations on variables and values.
If you’re starting your Kotlin journey, understanding operators will help you write logic, perform calculations, compare values, and control program flow effectively.
In this SEO-optimized, beginner-friendly guide, you’ll learn:
What operators are in Kotlin
Types of operators
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Unary operators
Bitwise operators
Elvis and safe-call operators
Real-world examples
Common beginner mistakes
Let’s begin
What Are Operators in Kotlin?
Operators are symbols that tell the compiler to perform specific mathematical, logical, or comparison operations.
Example:
Here:
+is an operator10and5are operands
Operators work with variables and values to produce results.
Types of Operators in Kotlin
Kotlin provides several types of operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Unary Operators
Bitwise Operators
Null Safety Operators
Let’s explore each category in detail.
Arithmetic Operators in Kotlin
Arithmetic operators perform mathematical calculations.
| Operator | Meaning |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
Example: Arithmetic Operators
Output:
Assignment Operators in Kotlin
Assignment operators assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
| = | x = 5 | Assign value |
| += | x += 3 | Add and assign |
| -= | x -= 2 | Subtract and assign |
| *= | x *= 2 | Multiply and assign |
| /= | x /= 2 | Divide and assign |
| %= | x %= 2 | Modulus and assign |
Example
Output:
Comparison Operators in Kotlin
Comparison operators compare two values and return Boolean results.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater or equal |
| <= | Less or equal |
Example
Output:
Logical Operators in Kotlin
Logical operators combine multiple conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| ! | Logical NOT |
Example
Output:
Logical operators are commonly used in decision-making.
Unary Operators in Kotlin
Unary operators operate on a single operand.
| Operator | Meaning |
|---|---|
| + | Positive |
| – | Negative |
| ++ | Increment |
| — | Decrement |
| ! | Logical NOT |
Example
Output:
Pre-Increment vs Post-Increment
Difference:
++x→ increments firstx++→ increments after use
Bitwise Operators in Kotlin
Kotlin does not use traditional bitwise symbols like & and |.
Instead, it provides functions:
| Function | Meaning |
|---|---|
| and | Bitwise AND |
| or | Bitwise OR |
| xor | Bitwise XOR |
| shl | Shift left |
| shr | Shift right |
Example
Null Safety Operators in Kotlin
Kotlin provides special operators for null safety.
Safe Call Operator (?.)
This prevents NullPointerException.
Elvis Operator (?:)
If left side is null, right side is returned.
Not-Null Assertion (!!)
Use carefully — may throw exception.
Real-World Example – Login System
Operators make this possible.
Operator Precedence in Kotlin
Order of execution:
Unary operators
Multiplication and division
Addition and subtraction
Comparison
Logical AND
Logical OR
Example:
Output:
Multiplication runs first.
Common Beginner Mistakes
Using = Instead of ==
= assigns value== compares value
Confusing && and ||
&& requires both conditions true|| requires only one
Ignoring Null Safety
Always use ?. or ?: when working with nullable variables.
Operators vs Functions
Some operators are actually translated into function calls.
Example:
Internally becomes:
Kotlin supports operator overloading.
When Should You Use Operators?
Use operators when:
Performing calculations
Comparing values
Combining conditions
Managing null values
Writing logical expressions
Keep expressions readable and simple.
Frequently Asked Questions (FAQs)
1. What are operators in Kotlin?
Operators are symbols used to perform operations on variables and values.
2. What is the difference between == and ===?
== checks structural equality.=== checks reference equality.
3. What is the Elvis operator?
The Elvis operator (?:) provides a default value if a variable is null.
4. What is the safe call operator?
The safe call operator (?.) safely accesses properties of nullable objects.
5. Does Kotlin support bitwise operators?
Yes, but they are implemented as functions like and, or, xor, shl, shr.
Conclusion
Kotlin operators are essential for writing logical, mathematical, and conditional expressions.
You learned:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Unary operators
Bitwise operations
Null safety operators
Mastering operators will make your Kotlin programming more efficient and powerful.
