codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

5 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Operators in Kotlin
🎯Kotlin

Operators in Kotlin

Updated 2026-04-20
3 min read

Operators in Kotlin

Operators are symbols that represent specific actions, such as arithmetic operations or logical comparisons, which can be performed on operands (values). Kotlin supports a wide range of operators that allow you to perform common tasks efficiently. This tutorial will cover various types of operators available in Kotlin, including arithmetic, comparison, assignment, and logical operators.

Arithmetic Operators

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

Basic Arithmetic Operations

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second. The result type depends on the types of the operands:
    • If both operands are integers, the result is an integer (integer division).
    • If at least one operand is a floating-point number, the result is a floating-point number.
  • Modulus (%): Returns the remainder of dividing the first operand by the second.
fun main() {
    val a = 10
    val b = 3

    println("Addition: ${a + b}") // Output: Addition: 13
    println("Subtraction: ${a - b}") // Output: Subtraction: 7
    println("Multiplication: ${a * b}") // Output: Multiplication: 30
    println("Division: ${a / b}") // Output: Division: 3 (integer division)
    println("Modulus: ${a % b}") // Output: Modulus: 1
}

Increment and Decrement Operators

Kotlin does not have built-in increment (++) or decrement (--) operators. However, you can achieve similar functionality using the inc() and dec() functions.

fun main() {
    var x = 5
    println("x before increment: $x") // Output: x before increment: 5

    x = x.inc()
    println("x after increment: $x") // Output: x after increment: 6

    x = x.dec()
    println("x after decrement: $x") // Output: x after decrement: 5
}

Comparison Operators

Comparison operators are used to compare two values and return a Boolean result.

  • Equal (==): Checks if two operands are equal.
  • Not Equal (!=): Checks if two operands are not equal.
  • Greater Than (>): Checks if the first operand is greater than the second.
  • Less Than (<): Checks if the first operand is less than the second.
  • Greater Than or Equal To (>=): Checks if the first operand is greater than or equal to the second.
  • Less Than or Equal To (<=): Checks if the first operand is less than or equal to the second.
fun main() {
    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
}

Assignment Operators

Assignment operators are used to assign values to variables. Kotlin supports compound assignment operators that combine arithmetic and assignment operations.

  • Simple Assignment (=): Assigns the value of the right operand to the left operand.
  • Compound Assignment: Combines an arithmetic operation with assignment:
    • +=, -=: Addition and subtraction
    • *=: Multiplication
    • /=: Division
    • %=: Modulus
fun main() {
    var a = 10

    println("a before += 5: $a") // Output: a before += 5: 10
    a += 5
    println("a after += 5: $a") // Output: a after += 5: 15

    println("a before -= 3: $a") // Output: a before -= 3: 15
    a -= 3
    println("a after -= 3: $a") // Output: a after -= 3: 12

    println("a before *= 4: $a") // Output: a before *= 4: 12
    a *= 4
    println("a after *= 4: $a") // Output: a after *= 4: 48

    println("a before /= 6: $a") // Output: a before /= 6: 48
    a /= 6
    println("a after /= 6: $a") // Output: a after /= 6: 8

    println("a before %= 3: $a") // Output: a before %= 3: 8
    a %= 3
    println("a after %= 3: $a") // Output: a after %= 3: 2
}

Logical Operators

Logical operators are used to combine multiple conditions and return a Boolean result.

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one operand is true.
  • Logical NOT (!): Inverts the value of the operand (true becomes false, and vice versa).
fun main() {
    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
}

Bitwise Operators

Bitwise operators operate on the binary representation of integers.

  • Bitwise AND (&): Performs a bitwise AND operation.
  • Bitwise OR (|): Performs a bitwise OR operation.
  • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation.
  • Bitwise NOT (!): Inverts the bits of the operand.
  • Left Shift (<<): Shifts the bits of the first operand to the left by the number of positions specified by the second operand.
  • Right Shift (>>): Shifts the bits of the first operand to the right by the number of positions specified by the second operand.
fun main() {
    val a = 6 // Binary: 0110
    val b = 3 // Binary: 0011

    println("a & b: ${a and b}") // Output: a & b: 2 (Binary: 0010)
    println("a | b: ${a or b}") // Output: a | b: 7 (Binary: 0111)
    println("a ^ b: ${a xor b}") // Output: a ^ b: 5 (Binary: 0101)
    println("!a: ${a.inv()}") // Output: !a: -7 (Binary: ...11111111111111111111111111111010)

    println("a << 2: ${a shl 2}") // Output: a << 2: 24 (Binary: 11000)
    println("a >> 1: ${a shr 1}") // Output: a >> 1: 3 (Binary: 00011)
}

Range and Collection Operators

Kotlin provides operators for working with ranges and collections.

  • Range (..): Creates a range from the first operand to the second operand.
  • Inclusive Range (until): Creates a range from the first operand up to but not including the second operand.
  • Contains (in): Checks if an element is contained within a collection or range.
fun main() {
    val range = 1..5
    println("Range: $range") // Output: Range: 1..5

    val inclusiveRange = 1 until 5
    println("Inclusive Range: $inclusiveRange") // Output: Inclusive Range: 1 until 5

    val numbers = listOf(1, 2, 3, 4, 5)
    println("Is 3 in numbers? ${3 in numbers}") // Output: Is 3 in numbers? true
}

Operator Overloading

Kotlin allows you to overload operators for custom classes. This means you can define how certain operators behave when used with instances of your class.

data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(this.x + other.x, this.y + other.y)
    }
}

fun main() {
    val p1 = Point(1, 2)
    val p2 = Point(3, 4)

    val p3 = p1 + p2
    println("p3: $p3") // Output: p3: Point(x=4, y=6)
}

Best Practices

  • Use Descriptive Names: When overloading operators, ensure that the behavior of the operator is intuitive and aligns with its standard meaning.
  • Avoid Overuse: While operator overloading can make code more readable, it should be used judiciously to avoid confusion.
  • Consistency: Ensure that overloaded operators behave consistently across your application.

Conclusion

Kotlin provides a rich set of operators that facilitate various operations in your applications. Understanding and effectively using these operators can lead to cleaner and more efficient code. By leveraging arithmetic, comparison, assignment, logical, bitwise, range, and collection operators, you can write expressive and concise Kotlin programs.

In the next section, we will explore control flow structures in Kotlin, allowing you to create more complex logic based on conditions and loops.


PreviousVariables and Data TypesNext Control Flow Statements

Recommended Gear

Variables and Data TypesControl Flow Statements