Kotlin Operators

Operators in Kotlin

Kotlin Operators are Arithmetic Operators, Assignment Operators, Comparison Operators, and Logical Operators.

In Kotlin language, operators are used to perform operations on given values. The operations could be mathematical, logical, etc. Based on the operation and requirement, the input values can be numeric, boolean, strings, etc.

For example, Arithmetic Addition operator takes two numeric values as operands, finds the sum of the two given numbers, and returns the result.

In this tutorial, we will go through all of the operators available in Kotlin, with examples, and references to the individual tutorials for each of the operators.

Arithmetic Operators

The following table covers Arithmetic Operators in Kotlin.

OperatorSymbolExampleDescription
Addition+x+yReturns the sum of x and y.
Subtraction-x-yReturns the subtraction of y from x.
Multiplication*x*yReturns the product of x and y.
Division/x/yReturns the quotient in the division of x by y.
Modulus%x%yReturns the remainder in the division of x by y.
Increment++++xReturns the value of x incremented by 1.
Decrement----xReturns the value of x decremented by 1.

In the following program, we take two numbers: x, and y; and perform Arithmetic Operations on these numbers.

Kotlin Program

fun main() {
    var x = 5
    var y = 2

    val addValue = x + y
    println("x + y = $addValue")

    val subValue = x - y
    println("x - y = $subValue")

    val mulValue = x * y
    println("x * y = $mulValue")

    val divValue = x / y
    println("x / y = $divValue")

    val modValue = x % y
    println("x % y = $modValue")

    ++x
    println("++x = $x")

    --y
    println("--y = $y")
}

Output

x + y = 7
x - y = 3
x * y = 10
x / y = 2
x % y = 1
++x = 6
--y = 1

The following tutorials cover Arithmetic Operators in detail with syntax and examples.

Assignment Operators

The following table covers Assignment Operators in Kotlin.

OperatorSymbolExampleDescription
Assignment=x=5Assign a value of 5 to x.
Addition Assignment+=x+=5Add a value of 5 to that of in x.
Subtraction Assignment-=x-=5Subtract a value of 5 from that of in x.
Multiplication Assignment*=x*=5Multiply a value of 5 to that of in x.
Division Assignment/=x/=5Divide the value in x with a value of 5.
Modulus Assignment%=x%=5Find remainder in the division of x/5 and assign the remainder to x.

In the following program, we take a variable x, and apply different assignment operators on it with a value of 5.

Kotlin Program

fun main() {
    var x: Int

    // Assignment
    x = 21
    println("x  = $x    Assignment")

    x = 21
    // Addition Assignment
    x += 5
    println("x  = $x   Addition Assignment")

    x = 21
    // Subtraction Assignment
    x -= 5
    println("x  = $x   Subtraction Assignment")

    x = 21
    // Multiplication Assignment
    x *= 5
    println("x  = $x   Multiplication Assignment")

    x = 21
    // Division Assignment
    x /= 5
    println("x  = $x   Division Assignment")

    x = 21
    // Modulus Assignment
    x %= 5
    println("x  = $x   Modulus Assignment")
}

Output

x  = 21    Assignment
x  = 26   Addition Assignment
x  = 16   Subtraction Assignment
x  = 105   Multiplication Assignment
x  = 4   Division Assignment
x  = 1   Modulus Assignment

The following tutorials cover Assignment Operators in detail.

Comparison Operators

The following table covers Comparison Operators in Kotlin.

OperatorSymbolExampleDescription
Equal-to==x==yReturns true if x and y are equal in value, or else returns false.
Not-equal!=x!=yReturns true if x and y are not equal in value, or else returns false.
Greater-than>x>yReturns true if value in x is greater than that of in y, or else returns false.
Less-than<x<yReturns true if value in x is less than that of in y, or else returns false.
Greater-than or Equal-to>=x>=yReturns true if value in x is greater than or equal to that of in y, or else returns false.
Less-than or Equal-to<=x<=yReturns true if value in x is less than or equal to that of in y, or else returns false.

In the following program, we take two numbers: a, and b; and perform comparison operations on these numbers.

Kotlin Program

fun main() {
    var x = 5
    var y = 2

    println("x = $x\ny = $y")

    var result: Boolean

    // Equal-to
    result = x == y
    println("x == y   $result")

    // Not-equal
    result = x != y
    println("x != y   $result")

    // Greater-than
    result = x > y
    println("x > y    $result")

    // Less-than
    result = x < y
    println("x < y    $result")

    // Greater-than or Equal-to
    result = x >= y
    println("x >= y   $result")

    // Less-than or Equal-to
    result = x <= y
    println("x <= y   $result")
}

Output

x = 5
y = 2
x == y   false
x != y   true
x > y    true
x < y    false
x >= y   true
x <= y   false

The following tutorials cover Comparison Operators in detail.

Logical Operators

The following table covers Logical Operators in Kotlin.

OperatorSymbolExampleDescription
AND&&x&&yReturns true if both x and y are true, else returns false.
OR||x||yReturns true if at least one of x or y is true, else returns false.
NOT!!xReturns true if x is false, or false if x is true.

In the following program, we take boolean values in a, and b; and perform logical operations on these values.

Kotlin Program

fun main() {
    val x = true
    val y = false
    println("x && y = ${x && y}")
    println("x || y = ${x || y}")
    println("!x = ${!x}")
}

Output

x && y = false
x || y = true
!x = false

The following tutorials cover Logical Operators in detail with syntax and examples.

More Tutorials on Operators

These are some more tutorials related to Kotlin Operators.