Kotlin Operators
Kotlin Operators
Operators in Kotlin are special symbols used to perform operations on variables and values. Kotlin provides a rich set of operators that are easy to understand and use.
1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus | a % b |
2. Assignment Operators
Used to assign values.
| Operator | Example |
|---|---|
= |
a = 5 |
+= |
a += 2 |
-= |
a -= 2 |
*= |
a *= 2 |
/= |
a /= 2 |
%= |
a %= 2 |
3. Comparison (Relational) Operators
Used to compare values.
| Operator | Description |
|---|---|
== |
Equal to |
!= |
Not equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
4. Logical Operators
Used to combine conditions.
| Operator | Description |
|---|---|
&& |
AND |
| ` | |
! |
NOT |
5. Unary Operators
Used with a single operand.
| Operator | Description |
|---|---|
++ |
Increment |
-- |
Decrement |
+ |
Unary plus |
- |
Unary minus |
! |
Logical NOT |
6. Range Operators
Used to create ranges.
| Operator | Example |
|---|---|
.. |
1..5 |
until |
1 until 5 |
downTo |
5 downTo 1 |
step |
1..10 step 2 |
7. Bitwise Operators (Functions)
Kotlin uses functions instead of symbols.
| Function | Description |
|---|---|
shl |
Shift left |
shr |
Shift right |
ushr |
Unsigned shift |
and |
Bitwise AND |
or |
Bitwise OR |
xor |
Bitwise XOR |
inv |
Invert bits |
8. Elvis Operator ?:
Used for null handling.
9. Safe Call Operator ?.
Summary
-
Kotlin has powerful and readable operators
-
Supports null-safety operators
-
Bitwise operations use functions
-
Range operators simplify loops
