
Operators in Kotlin
📌 Operators in Kotlin
Operators in Kotlin are special symbols or keywords used to perform operations on variables and values. Kotlin provides a variety of operators for mathematical calculations, logical comparisons, and more.
✅ 1. Types of Operators in Kotlin
Kotlin supports the following categories of operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Unary Operators
Increment and Decrement Operators
Type Check and Type Cast Operators
Range Operators
✅ 2. Arithmetic Operators
Used for mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 7 - 4 | 3 |
* | Multiplication | 6 * 3 | 18 |
/ | Division | 10 / 2 | 5 |
% | Modulus (Remainder) | 10 % 3 | 1 |
📌 Example:
fun() { val a = 10val b = 4 println("Addition: ${a + b}") println(") println("Multiplication: ${a * b}") println(") println("Modulus: ${a % b}")}
✅ 3. Assignment Operators
Used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= | a = 10 | a = 10 |
+= | a += 5 | a = a + 5 |
-= | a -= 3 | a = a - 3 |
*= | a *= 4 | a = a * 4 |
/= | a /= 2 | a = a / 2 |
%= | a %= 3 | a = a % 3 |
✅ 4. Comparison Operators
Used to compare two values. They return true
or false
.
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 4 | true |
< | Less than | 3 < 5 | true |
> | Greater than | 7 > 2 | true |
<= | Less than or equal to | 5 <= 5 | true |
>= | Greater than or equal | 8 >= 6 | true |
✅ 5. Logical Operators
Used to perform logical operations between Boolean values.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND (Both true) | true && false | false |
` | ` | Logical OR (At least one true) | |
! | Logical NOT (Negation) | !true | false |
✅ 6. Bitwise Operators
Bitwise operators are used to manipulate bits at a binary level.
Operator | Description | Example | Output |
---|---|---|---|
and | Bitwise AND | 5 and 3 | 1 |
or | Bitwise OR | 5 or 3 | 7 |
xor | Bitwise XOR | 5 xor 3 | 6 |
inv | Bitwise Inversion | 5.inv() | -6 |
shl | Left Shift | 5 shl 1 | 10 |
shr | Right Shift | 5 shr 1 | 2 |
✅ 7. Unary Operators
These operators act on a single operand.
Operator | Description | Example | Output |
---|---|---|---|
+ | Unary plus | +5 | 5 |
- | Unary minus | -5 | -5 |
++ | Increment Operator | a++ | a+1 |
-- | Decrement Operator | a-- | a-1 |
! | Logical NOT | !true | false |
✅ 8. Type Check and Cast Operators
Used to check or cast object types.
Operator | Description | Example | Output |
---|---|---|---|
is | Check if an object is of a certain type | x is String | true |
!is | Check if an object is not of a certain type | x !is Int | true |
as | Cast an object to a specific type | x as String | - |
✅ 9. Range Operator
The ..
operator is used to create a range of numbers.
📌 Example:
for (i in1..5) { println(i)}
Output:
12345
1..5
→ Includes both 1 and 5.until
→ Excludes the upper bound (1 until 5
→ 1 to 4).
✅ Conclusion
Arithmetic Operators → Perform mathematical operations.
Assignment Operators → Assign values with operations.
Comparison Operators → Compare values and return Boolean results.
Logical Operators → Perform Boolean logic operations.
Bitwise Operators → Work with binary data.
Unary Operators → Act on a single operand.
Type Operators → Perform type checking and casting.
Range Operator → Create number ranges for loops.