Skip to main content

Kotlin Operators

In this article, we will explore the Kotlin Operators. Operators are special symbols or keywords that carry out operations on operands (variables and values). Kotlin has a rich set of operators which makes it easy to perform many operations, such as mathematical, assignment, comparison, and more.

Arithmetic Operators

Arithmetic Operators are used to perform mathematical operations like addition, subtraction, multiplication, division, etc. Here's the list of arithmetic operators:

  • + : Adds two numbers
  • - : Subtracts two numbers
  • * : Multiplies two numbers
  • / : Divides two numbers
  • % : Returns the remainder of two numbers
val a = 10
val b = 5
println("a + b = ${a + b}") // Output: a + b = 15
println("a - b = ${a - b}") // Output: a - b = 5
println("a * b = ${a * b}") // Output: a * b = 50
println("a / b = ${a / b}") // Output: a / b = 2
println("a % b = ${a % b}") // Output: a % b = 0

Assignment Operators

Assignment Operators are used to assign values to variables. Below are the assignment operators:

  • = : Assigns values from right side operands to left side operand
  • += : It adds right operand to the left operand and assign the result to left operand
  • -= : It subtracts right operand from the left operand and assign the result to left operand
  • *= : It multiplies right operand with the left operand and assign the result to left operand
  • /= : It divides left operand with the right operand and assign the result to left operand
  • %= : It takes modulus using two operands and assign the result to left operand
val a = 10
var b = 5
b += a
println("b = $b") // Output: b = 15
b -= a
println("b = $b") // Output: b = 5
b *= a
println("b = $b") // Output: b = 50
b /= a
println("b = $b") // Output: b = 5
b %= a
println("b = $b") // Output: b = 5

Comparison Operators

Comparison Operators are used to compare two values. Here is a list of comparison operators:

  • == : Checks if the values of two operands are equal
  • != : Checks if the values of two operands are not equal
  • > : Checks if the value of left operand is greater than the value of right operand
  • < : Checks if the value of left operand is less than the value of right operand
  • >= : Checks if the value of left operand is greater than or equal to the value of right operand
  • <= : Checks if the value of left operand is less than or equal to the value of right operand
val a = 10
val b = 5
println("a == b : ${a == b}") // Output: a == b : false
println("a != b : ${a != b}") // Output: a != b : true
println("a > b : ${a > b}") // Output: a > b : true
println("a < b : ${a < b}") // Output: a < b : false
println("a >= b : ${a >= b}") // Output: a >= b : true
println("a <= b : ${a <= b}") // Output: a <= b : false

Logical Operators

Logical Operators are used to perform logical operations. Here's the list:

  • && : Logical AND (true if both the Boolean expressions are true)
  • || : Logical OR (true if at least one Boolean expression is true)
  • ! : Logical NOT (reverses the truth-value)
val a = true
val b = false
println("a && b : ${a && b}") // Output: a && b : false
println("a || b : ${a || b}") // Output: a || b : true
println("!a : ${!a}") // Output: !a : false
println("!b : ${!b}") // Output: !b : true

Conclusion

Operators in Kotlin are powerful and flexible, allowing you to perform a wide array of operations. Understanding how and when to use them is crucial when learning Kotlin programming. In this tutorial, we've covered the basics of Kotlin Operators. Practice and experimentation are key, so try these out in different combinations to get a feel for how they work.