Kotlin Operators Tutorial: Arithmetic, Relational, Logical, Assignment, Unary, in and is Operators


This Kotlin Operators tutorial explains all major operators used in Kotlin programming, including arithmetic, relational, logical, assignment, unary, and special operators such as in and is. Each operator type is explained with clear syntax, practical examples, and best practices to help beginners and professionals write correct, readable, and efficient Kotlin code.

Kotlin Operators – Complete Tutorial

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Operators

  1. + Addition
  2. - Subtraction
  3. * Multiplication
  4. / Division
  5. % Modulus

Example


val a = 10
val b = 3

println(a + b)
println(a - b)
println(a * b)
println(a / b)
println(a % b)

Best Practices

  1. Use meaningful variable names for clarity.
  2. Be careful with integer division; use Double if precision is required.

Relational Operators

Relational operators compare two values and return a Boolean result.

Operators

  1. > Greater than
  2. < Less than
  3. >= Greater than or equal to
  4. <= Less than or equal to
  5. == Equal to
  6. != Not equal to

Example


val x = 10
val y = 20

println(x > y)
println(x < y)
println(x == y)
println(x != y)

Best Practices

  1. Use == for value comparison, not reference comparison.
  2. Avoid complex comparisons in a single statement.

Logical Operators

Logical operators are used to combine multiple Boolean expressions.

Operators

  1. && Logical AND
  2. || Logical OR
  3. ! Logical NOT

Example


val isAdult = true
val hasId = false

println(isAdult && hasId)
println(isAdult || hasId)
println(!isAdult)

Best Practices

  1. Keep Boolean expressions simple and readable.
  2. Use parentheses for clarity when needed.

Assignment Operators

Assignment operators assign values to variables.

Operators

  1. = Assign
  2. += Add and assign
  3. -= Subtract and assign
  4. *= Multiply and assign
  5. /= Divide and assign
  6. %= Modulus and assign

Example


var count = 10
count += 5
count -= 2
count *= 2
count /= 2

Best Practices

  1. Use compound assignment operators to reduce verbosity.
  2. Avoid modifying the same variable excessively in one block.

Unary Operators

Unary operators operate on a single operand.

Operators

  1. + Unary plus
  2. - Unary minus
  3. ++ Increment
  4. -- Decrement
  5. ! Logical NOT

Example


var number = 5

println(+number)
println(-number)
number++
println(number)
number--
println(number)

Best Practices

  1. Prefer readable expressions over excessive increment or decrement usage.
  2. Avoid using ++ and -- inside complex expressions.

in Operator

The in operator checks whether a value exists within a range or collection.

Example with Range


val num = 7

if (num in 1..10) {
println("Number is in range")
}

Example with Collection


val languages = listOf("Kotlin", "Java", "Python")

if ("Kotlin" in languages) {
println("Kotlin is available")
}

Best Practices

  1. Use in for clean and readable range checks.
  2. Prefer in over manual loops.

is Operator

The is operator checks the type of an object at runtime.

Example


fun checkType(value: Any) {
if (value is String) {
println("Length: ${value.length}")
}
}

Best Practices

  1. Use is for safe type checking.
  2. Take advantage of smart casting after is checks.

Summary

This chapter covered all essential Kotlin operators, including arithmetic, relational, logical, assignment, unary, and special operators like in and is. Understanding these operators is fundamental for writing logical conditions, loops, and expressions in Kotlin programs.