Kotlin Operators Reference 🎯

beginner
5 min

Kotlin Operators Reference 🎯

Welcome to the Kotlin Operators Reference! In this comprehensive guide, we'll explore various operators available in Kotlin, their uses, and examples to help you become a proficient Kotlin developer. Let's dive right in!

Arithmetic Operators 💡

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.

Addition (+)

Adds two operands together.

kotlin
val a = 5 val b = 3 val sum = a + b // sum = 8

Subtraction (-)

Subtracts one operand from another.

kotlin
val a = 5 val b = 3 val difference = a - b // difference = 2

Multiplication (*)

Multiplies two operands together.

kotlin
val a = 5 val b = 3 val product = a * b // product = 15

Division (/)

Divides one operand by another.

kotlin
val a = 10 val b = 2 val quotient = a / b // quotient = 5.0

Modulus (%)

Returns the remainder of the division operation.

kotlin
val a = 10 val b = 3 val remainder = a % b // remainder = 1

Assignment Operators 📝

Assignment operators are used to assign a value to a variable.

Assignment (=)

Assigns the right operand to the left operand.

kotlin
val a = 5

Compound Assignment

Performs an operation and assigns the result back to the variable.

kotlin
val a = 5 a += 3 // a = 8

Comparison Operators 📝

Comparison operators are used to compare two operands.

Equality (==)

Checks if two operands are equal.

kotlin
val a = 5 val b = 5 if (a == b) println("a is equal to b")

Inequality (!=)

Checks if two operands are not equal.

kotlin
val a = 5 val b = 6 if (a != b) println("a is not equal to b")

Greater Than (>)

Checks if the left operand is greater than the right operand.

kotlin
val a = 7 val b = 5 if (a > b) println("a is greater than b")

Less Than (<)

Checks if the left operand is less than the right operand.

kotlin
val a = 3 val b = 5 if (a < b) println("a is less than b")

Greater Than or Equal (>=)

Checks if the left operand is greater than or equal to the right operand.

kotlin
val a = 7 val b = 5 if (a >= b) println("a is greater than or equal to b")

Less Than or Equal (<=)

Checks if the left operand is less than or equal to the right operand.

kotlin
val a = 3 val b = 5 if (a <= b) println("a is less than or equal to b")

Quiz 💡

Quick Quiz
Question 1 of 1

Which operator is used to perform division in Kotlin?


Hope you enjoyed learning about Kotlin operators! In the next lesson, we'll dive deeper into control flow structures like loops and conditional statements. Until then, happy coding! 🚀