Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin Operators

Kotlin provides a rich set of operators that allow you to perform a variety of operations. In this tutorial, we'll provide an overview of the most commonly used operators in Kotlin.

1. Arithmetic Operators

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

2. Comparison Operators

  • ==: Checks for structural equality
  • !=: Checks for structural inequality
  • ===: Checks for referential equality (i.e., whether two references point to the same object)
  • !==: Checks for referential inequality
  • <: Less than
  • <=: Less than or equal to
  • >: Greater than
  • >=: Greater than or equal to

3. Assignment Operators

  • =: Assigns a value
  • +=: Adds and assigns
  • -=: Subtracts and assigns
  • *=: Multiplies and assigns
  • /=: Divides and assigns
  • %=: Takes modulus and assigns

4. Unary Operators

  • +: Indicates a positive number (e.g., +3)
  • -: Negates a number (e.g., -3)
  • ++: Increment
  • --: Decrement
  • !: Logical negation

5. Logical Operators

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

6. in Operator

  • in: Checks if a value is present in a range, collection, etc.
  • !in: Checks if a value is not present

7. is Operator

Used for type checking.

  • is: Checks if an object is of a given type
  • !is: Checks if an object is not of a given type

8. Bitwise Operators (Available for Int and Long)

  • shl: Shifts left
  • shr: Shifts right
  • ushr: Unsigned shift right
  • and: Bitwise AND
  • or: Bitwise OR
  • xor: Bitwise XOR
  • inv: Bitwise inversion

9. Other Operators

  • ?: (Elvis operator): Used for null safety (e.g., val length = name?.length ?: 0)
  • ?. (Safe call operator): Performs the call only if the receiver is non-null.
  • !! (Non-null assertion operator): Throws a NullPointerException if the value is null.
  • :: (Reflection operator): References a class, function, or property.

Examples:

// Arithmetic
val sum = 5 + 3

// Comparison
val isEqual = (sum == 8)

// Assignment
var number = 10
number += 5

// Unary
val negation = -number

// Logical
val result = (sum > 0) && (number > 0)

// 'in' operator
val withinRange = 5 in 1..10

// 'is' operator
val isString = "Hello" is String

// Bitwise
val shifted = number shl 2

Summary

Kotlin offers a wide range of operators for various operations, making the code expressive and concise. Proper understanding and use of these operators can enhance the clarity and efficiency of your Kotlin code.

  1. Arithmetic operators in Kotlin:

    • Common arithmetic operators include +, -, *, /, %.
    val sum = 5 + 3
    val difference = 5 - 3
    val product = 5 * 3
    val quotient = 5 / 3
    val remainder = 5 % 3
    
  2. Comparison operators in Kotlin:

    • Comparison operators include ==, !=, >, <, >=, <=.
    val isEqual = (5 == 3)
    val isNotEqual = (5 != 3)
    val isGreaterThan = (5 > 3)
    val isLessThan = (5 < 3)
    
  3. Logical operators in Kotlin:

    • Logical operators include && (and), || (or), ! (not).
    val andResult = true && false
    val orResult = true || false
    val notResult = !true
    
  4. Bitwise operators in Kotlin:

    • Bitwise operators include and, or, xor, shl, shr, ushr.
    val bitwiseAnd = 5 and 3
    val bitwiseOr = 5 or 3
    val bitwiseXor = 5 xor 3
    val leftShift = 5 shl 1
    val rightShift = 5 shr 1
    
  5. Assignment operators in Kotlin:

    • Assignment operators include =, +=, -=, *=, /=, %=.
    var num = 5
    num += 3
    num -= 2
    num *= 4
    num /= 2
    num %= 3
    
  6. Unary operators in Kotlin:

    • Unary operators include +, -.
    val positiveNum = +5
    val negativeNum = -5
    
  7. Overloading operators in Kotlin:

    • Custom classes can overload operators for custom behavior.
    data class Complex(val real: Double, val imag: Double) {
        operator fun plus(other: Complex) = Complex(real + other.real, imag + other.imag)
    }
    
  8. Custom operators in Kotlin:

    • Create custom operators using functions with specific names.
    data class Quantity(val value: Int) {
        operator fun times(other: Quantity) = Quantity(value * other.value)
    }
    
  9. Operator precedence and associativity in Kotlin:

    • Operators have precedence and associativity rules, similar to mathematics.
    val result = 5 + 3 * 2 // Result is 11, considering precedence
    
  10. Using operators with different data types in Kotlin:

    • Kotlin provides type coercion for certain operations.
    val result = 5 + 3.0 // Result is 8.0, Int is promoted to Double
    
  11. Null safety and operators in Kotlin:

    • Operators can be safely used with nullable types, thanks to Kotlin's null safety.
    val nullableInt: Int? = null
    val result = nullableInt?.plus(5) // Result is null if nullableInt is null