Kotlin Operators - A Quick Introduction


Operators are essential elements in programming that allow you to perform various operations on data. In Kotlin, you'll find a wide range of operators that help you manipulate values, compare them, and perform mathematical calculations. In this quick introduction, we'll explore some of the most commonly used operators in Kotlin.


Arithmetic Operators

Kotlin supports the standard arithmetic operators:

val a = 10
val b = 5
val sum = a + b // Addition
val difference = a - b // Subtraction
val product = a * b // Multiplication
val quotient = a / b // Division
val remainder = a % b // Modulo (remainder)

Comparison Operators

Comparison operators are used to compare values and produce boolean results:

val x = 10
val y = 20
val isEqual = x == y // Equal to
val isNotEqual = x != y // Not equal to
val isGreater = x > y // Greater than
val isLess = x < y // Less than
val isGreaterOrEqual = x >= y // Greater than or equal to
val isLessOrEqual = x <= y // Less than or equal to

Logical Operators

Logical operators are used to work with boolean values:

val isTrue = true
val isFalse = false
val andResult = isTrue && isFalse // Logical AND
val orResult = isTrue || isFalse // Logical OR
val notResult = !isTrue // Logical NOT

Assignment Operators

Assignment operators are used to assign values to variables:

var count = 5
count += 3 // Increment by 3
count -= 2 // Decrement by 2
count *= 4 // Multiply by 4
count /= 2 // Divide by 2
count %= 3 // Assign remainder after division by 3

Conclusion

Kotlin operators are powerful tools for manipulating and working with data. Understanding how to use these operators is a fundamental part of writing Kotlin programs. As you continue to explore Kotlin, you'll encounter more operators and advanced techniques for efficient data manipulation in your applications.


Happy coding!