Basic Operators in Kotlin

A comprehensive guide to learning Kotlin programming from basics to advanced concepts

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:

OperatorDescriptionExample
+Additionval sum = 5 + 3 // 8
-Subtractionval diff = 5 - 3 // 2
*Multiplicationval product = 5 * 3 // 15
/Divisionval 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).

OperatorDescriptionExample
==Equal toval isEqual = (5 == 5) // true
!=Not equal toval isNotEqual = (5 != 3) // true
>Greater thanval isGreater = (5 > 3) // true
<Less thanval isLesser = (5 < 10) // true
>=Greater than or equal toval isGreaterOrEqual = (5 >= 5) // true
<=Less than or equal toval 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.

OperatorDescriptionExample
&&Logical ANDval result = (5 > 3 && 10 > 5) // true
``
!Logical NOTval 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.

OperatorDescriptionExample
=Simple assignmentvar a = 10
+=Addition assignmenta += 5 // a = a + 5
-=Subtraction assignmenta -= 3 // a = a - 3
*=Multiplication assignmenta *= 2 // a = a * 2
/=Division assignmenta /= 4 // a = a / 4
%=Modulus assignmenta %= 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.

OperatorDescriptionExample
shlLeft shiftval result = 4 shl 1 // 8
shrRight shiftval result = 4 shr 1 // 2
ushrUnsigned right shiftval result = -4 ushr 1
andBitwise ANDval result = 4 and 2 // 0
orBitwise ORval result = 4 or 2 // 6
xorBitwise XORval result = 4 xor 2 // 6
invBitwise NOTval 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.


Last modified 20.02.2025: new kotlin and mint content (93a1000)