Basic Operators in Kotlin

Categories:
4 minute read
Introduction
Kotlin, a modern and expressive programming language developed by JetBrains, is widely adopted for Android development and general-purpose programming. One of Kotlin’s strengths is its support for a variety of operators that enable developers to perform operations efficiently. Operators in Kotlin are categorized based on their functionalities, such as arithmetic, relational, logical, assignment, and bitwise operations. Understanding these operators is essential for writing concise and effective Kotlin programs.
This blog post explores the fundamental operators in Kotlin, their syntax, and practical usage examples to help you master the basics of Kotlin programming.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations. Kotlin supports the following arithmetic operators:
Operator | Description | Example |
---|---|---|
+ | Addition | val sum = 5 + 3 // 8 |
- | Subtraction | val diff = 5 - 3 // 2 |
* | Multiplication | val product = 5 * 3 // 15 |
/ | Division | val quotient = 10 / 2 // 5 |
% | Modulus (Remainder) | val remainder = 10 % 3 // 1 |
Example
fun main() {
val a = 10
val b = 4
println("Addition: ${a + b}")
println("Subtraction: ${a - b}")
println("Multiplication: ${a * b}")
println("Division: ${a / b}")
println("Modulus: ${a % b}")
}
2. Relational (Comparison) Operators
Relational operators are used to compare two values. These operators return a Boolean result (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | val isEqual = (5 == 5) // true |
!= | Not equal to | val isNotEqual = (5 != 3) // true |
> | Greater than | val isGreater = (5 > 3) // true |
< | Less than | val isLesser = (5 < 10) // true |
>= | Greater than or equal to | val isGreaterOrEqual = (5 >= 5) // true |
<= | Less than or equal to | val isLessOrEqual = (3 <= 5) // true |
Example
fun main() {
val x = 15
val y = 20
println("x is greater than y: ${x > y}")
println("x is less than or equal to y: ${x <= y}")
}
3. Logical Operators
Logical operators are used to perform logical operations, usually in conjunction with Boolean expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND | val result = (5 > 3 && 10 > 5) // true |
` | ` | |
! | Logical NOT | val result = !(5 == 5) // false |
Example
fun main() {
val isSunny = true
val isWeekend = false
println("Should go out: ${isSunny && isWeekend}")
}
4. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Simple assignment | var a = 10 |
+= | Addition assignment | a += 5 // a = a + 5 |
-= | Subtraction assignment | a -= 3 // a = a - 3 |
*= | Multiplication assignment | a *= 2 // a = a * 2 |
/= | Division assignment | a /= 4 // a = a / 4 |
%= | Modulus assignment | a %= 3 // a = a % 3 |
Example
fun main() {
var num = 10
num += 5
println("Updated num: $num")
}
5. Bitwise Operators
Bitwise operators perform operations at the binary level.
Operator | Description | Example |
---|---|---|
shl | Left shift | val result = 4 shl 1 // 8 |
shr | Right shift | val result = 4 shr 1 // 2 |
ushr | Unsigned right shift | val result = -4 ushr 1 |
and | Bitwise AND | val result = 4 and 2 // 0 |
or | Bitwise OR | val result = 4 or 2 // 6 |
xor | Bitwise XOR | val result = 4 xor 2 // 6 |
inv | Bitwise NOT | val result = 4.inv() |
Example
fun main() {
val num1 = 4
val num2 = 2
println("Bitwise AND: ${num1 and num2}")
}
Conclusion
Kotlin provides a rich set of operators that help developers perform calculations, comparisons, and logical operations efficiently. Understanding and utilizing these basic operators correctly enhances code readability and performance. Whether you’re working on mathematical computations, decision-making, or bitwise operations, these fundamental Kotlin operators will be an essential part of your programming journey.
By mastering these operators, you can write more concise, readable, and efficient Kotlin code, making your applications robust and maintainable.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.