Kotlin Operators

Introduction

Operators are special symbols that perform operations on variables and values. In Kotlin, operators are used for arithmetic calculations, comparisons, logical operations, and more. This chapter will cover the various types of operators available in Kotlin, along with examples and their syntax.

Types of Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operators:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus)

Syntax:

val result = operand1 operator operand2

Example:

fun main() {
    val a = 10
    val b = 5

    println("Addition: ${a + b}")      // Output: 15
    println("Subtraction: ${a - b}")   // Output: 5
    println("Multiplication: ${a * b}") // Output: 50
    println("Division: ${a / b}")      // Output: 2
    println("Modulus: ${a % b}")       // Output: 0
}

2. Comparison Operators

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

Operators:

  • == (Equal to)
  • != (Not equal to)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)

Syntax:

val result = operand1 operator operand2

Example:

fun main() {
    val a = 10
    val b = 5

    println("Equal to: ${a == b}")         // Output: false
    println("Not equal to: ${a != b}")     // Output: true
    println("Less than: ${a < b}")         // Output: false
    println("Greater than: ${a > b}")      // Output: true
    println("Less than or equal to: ${a <= b}") // Output: false
    println("Greater than or equal to: ${a >= b}") // Output: true
}

3. Logical Operators

Logical operators are used to combine multiple Boolean expressions.

Operators:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)

Syntax:

val result = operand1 operator operand2

Example:

fun main() {
    val a = true
    val b = false

    println("Logical AND: ${a && b}")  // Output: false
    println("Logical OR: ${a || b}")   // Output: true
    println("Logical NOT: ${!a}")      // Output: false
}

4. Assignment Operators

Assignment operators are used to assign values to variables.

Operators:

  • = (Assign)
  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)
  • %= (Modulus and assign)

Syntax:

variable operator value

Example:

fun main() {
    var a = 10
    println("Initial value: $a")  // Output: 10

    a += 5
    println("After += : $a")  // Output: 15

    a -= 3
    println("After -= : $a")  // Output: 12

    a *= 2
    println("After *= : $a")  // Output: 24

    a /= 4
    println("After /= : $a")  // Output: 6

    a %= 2
    println("After %= : $a")  // Output: 0
}

5. Bitwise Operators

Bitwise operators are used to perform bit-level operations.

Operators:

  • and (Bitwise AND)
  • or (Bitwise OR)
  • xor (Bitwise XOR)
  • inv (Bitwise inversion)
  • shl (Left shift)
  • shr (Right shift)
  • ushr (Unsigned right shift)

Syntax:

val result = operand1.operator(operand2)

Example:

fun main() {
    val a = 5  // 0101 in binary
    val b = 3  // 0011 in binary

    println("Bitwise AND: ${a and b}")  // Output: 1 (0001 in binary)
    println("Bitwise OR: ${a or b}")    // Output: 7 (0111 in binary)
    println("Bitwise XOR: ${a xor b}")  // Output: 6 (0110 in binary)
    println("Bitwise Inversion: ${a.inv()}")  // Output: -6 (1111...1010 in binary)

    println("Left shift: ${a shl 1}")   // Output: 10 (1010 in binary)
    println("Right shift: ${a shr 1}")  // Output: 2 (0010 in binary)
    println("Unsigned right shift: ${a ushr 1}")  // Output: 2 (0010 in binary)
}

Conclusion

In this chapter, you learned about the various types of operators available in Kotlin, including arithmetic, comparison, logical, assignment, and bitwise operators. Understanding these operators is essential for performing a wide range of operations in your Kotlin programs, allowing you to manipulate data and make decisions effectively.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top