Kotlin Operators: Kotlin has a set of operators to perform arithmetic, task, comparison operators, and more. You will learn to use these operators in this tutorial.

Operators are special characters which perform the procedure on operands (values or variable). There are different sorts of operators accessible in Kotlin.

Operators are special symbols (characters) that complete procedure on operands (variables and values). For instance, + is an operator that performs addition.

In the Java variables tutorial, you learned to declare variables and assign values to variables. Presently, you will learn to use operators to perform different procedure on them.


1. Arithmetic Operators

Here’s a list of arithmetic operators in Kotlin:

OperatorMeaning
+Addition (also used for string concatenation)
Subtraction Operator
*Multiplication Operator
/Division Operator
%Modulus Operator

Example: Arithmetic Operators

fun main(args: Array<String>) {

    val number1 = 12.5
    val number2 = 3.5
    var result: Double

    result = number1 + number2
    println("number1 + number2 = $result")

    result = number1 - number2
    println("number1 - number2 = $result")

    result = number1 * number2
    println("number1 * number2 = $result")

    result = number1 / number2
    println("number1 / number2 = $result")

    result = number1 % number2
    println("number1 % number2 = $result")
}

At the point when you run the program, the output will be:

number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43.75
number1 / number2 = 3.5714285714285716
number1 % number2 = 2.0

The + operator is additionally used for the concatenation of String values.


Example: Concatenation of Strings

fun main(args: Array<String>) {

    val start = "Talk is cheap. "
    val middle = "Show me the code. "
    val end = "- Linus Torvalds"

    val result = start + middle + end
    println(result)
}

At the point when you run the program, the output will be:

Talk is cheap. Show me the code. - Linus Torvalds

How arithmetic operators actually work?

Assume, you are using + arithmetic operator to add two numbers an and b.

Under the hood, the expression a + b calls a.plus(b) part work. The plus operator is over-burden to work with String values and other essential data types (except from Char and Boolean).

// + operator for basic types
operator fun plus(other: Byte): Int
operator fun plus(other: Short): Int
operator fun plus(other: Int): Int
operator fun plus(other: Long): Long
operator fun plus(other: Float): Float
operator fun plus(other: Double): Double

// for string concatenation
operator fun String?.plus(other: Any?): String

You can likewise use the + operator to work with user-characterized types (like objects) by over-burdening plus () function.

Here’s a table of arithmetic operators and their corresponding functions:

ExpressionFunction nameTranslates to
a + bplusa.plus(b)
a – bminusa.minus(b)
a * btimesa.times(b)
a / bdiva.div(b)
a % bmoda.mod(b)

2. Assignment Operators

Task administrators are utilized to allocate worth to a variable. We have effectively utilized straightforward task administrator = previously.

val age = 5

Here, 5 is assigned to variable age using = operator.

Here’s a list of all assignment operators and their corresponding functions:

ExpressionEquivalent toTranslates to
a +=ba = a + ba.plusAssign(b)
a -= ba = a – ba.minusAssign(b)
a *= ba = a * ba.timesAssign(b)
a /= ba = a / ba.divAssign(b)
a %= ba = a % ba.modAssign(b)

Example: Assignment Operators

fun main(args: Array<String>) {
    var number = 12

    number *= 5   // number = number*5
    println("number  = $number")
}

At the point when you run the program, the output will be:

number = 60

3. Unary prefix and Increment / Decrement Operators

Here’s a table of unary operators, their meaning, and corresponding functions:

OperatorMeaningExpressionTranslates to
+Unary plus+aa.unaryPlus()
Unary minus (inverts sign)-aa.unaryMinus()
!not (inverts value)!aa.not()
++Increment: increases value by1++aa.inc()
Decrement: decreases value by 1–aa.dec()

Example: Unary Operators

fun main(args: Array<String>) {
    val a = 1
    val b = true
    var c = 1

    var result: Int
    var booleanResult: Boolean

    result = -a
    println("-a = $result")

    booleanResult = !b
    println("!b = $booleanResult")

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

At the point when you run the program, the output will be:

-a = -1
!b = false
--c = 0

4. Comparison and Equality Operators

Here’s a table of equality and comparison operators, their meaning, and corresponding functions:

OperatorMeaningExpressionTranslates to
greater thana > ba.compareTo(b) > 0
less thana < ba.compareTo(b) < 0
>=greater than or equals toa >= ba.compareTo(b) >= 0
<=less than or equals toa < = ba.compareTo(b) <= 0
==is equal toa == ba?.equals(b) ?: (b === null)
!=not equal toa != b!(a?.equals(b) ?: (b === null))

Comparison and equity operators are used in the control flow, for example, if articulation, when articulation, and loops.


Example: Comparison and Equality Operators

fun main(args: Array<String>) {

    val a = -12
    val b = 12

    // use of greater than operator
    val max = if (a > b) {
        println("a is larger than b.")
        a
    } else {
        println("b is larger than a.")
        b
    }

    println("max = $max")
}

At the point when you run the program, the output will be:

b is larger than a.
max = 12

5. Logical Operators

There are two logical operators in Kotlin: || and &&

Here’s a table of logical operators, their meaning, and corresponding function .

OperatorDescriptionExpressionCorresponding Function
||true if either of the Boolean expression is true(a>b)||(a<c)(a>b)or(a<c)
&&true if all Boolean expressions are true(a>b)&&(a<c)(a>b)and(a<c)

Note that, or and are functions that support infix notation..

Logical operators are used in control flow, for example, if articulation, when articulation, and loops.


Example: Logical Operators

fun main(args: Array<String>) {

    val a = 10
    val b = 9
    val c = -1
    val result: Boolean

    // result is true is a is largest
    result = (a>b) && (a>c) // result = (a>b) and (a>c)
    println(result)
}

At the point when you run the program, the output will be:

true

6. in Operator


The in operator is used to check whether an object belongs to a collection.

OperatorExpressionTranslates to
ina in bb.contains(a)
!ina !in b!b.contains(a)

Example: in Operator

fun main(args: Array<String>) {

    val numbers = intArrayOf(1, 4, 42, -3)

    if (4 in numbers) {
        println("numbers array contains 4.")
    }
}

At the point when you run the program, the output will be:

numbers array contains 4.

7. Index access Operator

Here are a few articulations using index access operator with coresponding functions in Kotlin.

ExpressionTranslated to
a[i]a.get(i)
a[i, n]a.get(i, n)
a[i1, i2, …, in]a.get(i1, i2, …, in)
a[i] = ba.set(i, b)
a[i, n] = ba.set(i, n, b)
a[i1, i2, …, in] = ba.set(i1, i2, …, in, b)

Example: Index access Operator

fun main(args: Array<String>) {

    val a  = intArrayOf(1, 2, 3, 4, - 1)
    println(a[1])   
    a[1]= 12
    println(a[1])
}

At the point when you run the program, the output will be:

2
12

8. Invoke Operator

Here are some expressions using invoke operator with corresponding functions in Kotlin.

ExpressionTranslated to
a()a.invoke()
a(i)a.invoke(i)
a(i1, i2, …, in)a.inkove(i1, i2, …, in)
a[i] = ba.set(i, b)

In Kotlin, parenthesis are translated to call invoke member function.


Bitwise Operation

Unlike Java, there are no bitwise and bitshift operators in Kotlin. To play out these assignments, different functions (supporting infix notation) are used:

  • shl – Signed shift left
  • shr – Signed shift right
  • ushr – Unsigned shift right
  • and – Bitwise and
  • or – Bitwise or
  • xor – Bitwise xor
  • inv – Bitwise inversion

Additionally, there is no ternary administrator in Kotlin dissimilar to Java.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.