English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin Operators

Kotlin has a set of operators that can perform arithmetic, assignment, comparison, and more. You will learn how to use these operators in this article.

An operator is a special symbol (character) used to perform operations on operands (variables and values). For example,+ is the operator used for addition.

InKotlin VariablesIn this article, you learned how to declare variables and assign values to them. Now, you will learn how to perform various operations on them using operators.

Arithmetic Operators

The following is a list of arithmetic operators in Kotlin:

Kotlin Arithmetic Operators
The in operator is used to check if an object belongs to a collection.Description
+Addition Operator (also used for string concatenation)
-Subtraction Operator
*Multiplication Operator
/Division Operator
%Modulo Operator

Example: Arithmetic Operators

fun main(args: Array<String>) {
    val number1 = 12numbers array contains5
    val number2 = 3numbers array contains5
    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)
}

When running the program, the output is:

number1 + number2 = 16.0
number1 - number2 = 9.0
number1 * number2 = 43numbers array contains75
number1 / number2 = 3numbers array contains5714285714285716
number1 % number2 = 2.0

+ Operators can also be used for string chaining, concatenating strings together.

Example: string concatenation

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
    result = (a>b) and (a>c)
}

When running the program, the output is:

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

How do arithmetic operators actually work?

Suppose you are using + Arithmetic operators add two numbers a and b together.

Behind the scenes, the expression a + b calls the a.plus(b) member function. The plus operator is overloaded to handle String values and other basic data types (Char and Booleanexcept).

// + Basic type operators
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 also use + Operators handle user-defined types (such as objects) by overloading the plus() function.

Recommended reading: Kotlin Operator Overloading

This is a list of arithmetic operators and their corresponding functions:

operatorFunction nameUsage
a + bAdditiona.plus(b)
a-bSubtractiona.minus(b)
a * bMultiplication
a.times(b)
a / bDivision
a.div(b)
a % bModulusa.mod(b)

Assignment operators

Assignment operators are used to assign values to variables. We have already used the simple = assignment operator.

val age = 5

Here, the = operator is used to assign 5 Assign to the variable age.

The following is a list of all assignment operators and their corresponding functions:

operatoris equivalent toUsage
a + assign ba = a + ba.plusAssign(b)
a-assign ba = a-ba.minusAssign(b)
a * assign ba = a * ba.timesAssign(b)
a / assign ba = a / ba.assign(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")
}

When running the program, the output is:

number = 60

Unary prefix and increment/Decrement operator

This is a unary operator, their meanings and corresponding function table:

The in operator is used to check if an object belongs to a collection.DescriptionoperatorUsage
+Unary plus+aa.unaryPlus()
-Unary minus-aa.unaryMinus()
Not (reverses the value)!aa.not()
++Increment: Value plus1++aa.inc()
--Decrement: Value minus1--a
a.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")
}

When running the program, the output is:

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

Comparison operators and equality operators

This is an equality and comparison operator, their meanings and corresponding function list:

The in operator is used to check if an object belongs to a collection.DescriptionoperatorUsage
>Greater thana> ba.compareTo(b)> 0
<Less thana <ba.compareTo(b)<0
> =Greater than or equal toa> = ba.compareTo(b)>= 0
<=Less than or equal toa <= ba.compareTo(b)<= 0
==Equala == ba?.equals(b)?:(b === null)
!=Not equala!= b!(a?.equals(b)?:(b ===null))

Comparison and equality operators are used for control flow, such as:if expression,when expression and loop.

Example: Comparison and equality operators

fun main(args: Array<String>) {
    val a = -12
    val a = 12
    //Using the greater than operator
    val max = if (a > b) {
        println("a大于b")
        a
    } else {
        println("b大于a")
        b
    }
    println("max = $max")
}

When running the program, the output is:

b is greater than a
max = 12

Logical operator

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

This is a logical operator, their meanings and corresponding functions table.

The in operator is used to check if an object belongs to a collection.DescriptionoperatorCorresponding function
||If any boolean expression is true, it returns true
(a>b)||(a<c)(a>b)or(a<c)
&&It is true if all boolean expressions are true(a>b)&&(a<c)(a>b)and(a<c)

Please note that or and and are functions that support infix notation.

Logical operators are used for control flow, such as if expression,when expression and loop.

Example: Logical operators

fun main(args: Array<String>) {
    val a = 10
    val a = 9
    val b = -1
    val c =
    // val result: Boolean
    result is true if the largest // result = (a>b) && (a>c)
    result = (a>b) and (a>c)
}

When running the program, the output is:

println(result)

true

in operator

The in operator is used to check if an object belongs to a collection.operatorUsage
expression
ina in b
b.contains(a)
!in
a !in b

!b.contains(a)

fun main(args: Array<String>) {
    Example: in operator1, 4, 42, -3)
    val numbers = intArrayOf(4 if (
        in numbers) { 4println("numbers array contains
    }
}

When running the program, the output is:

."  4numbers array contains

.

The following are some expressions using the index access operator with corresponding functions in Kotlin.

expressionUsage
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])
}

When running the program, the output is:

2
12

invoke operator

The following are some expressions using the invoke operator with corresponding functions in Kotlin.

expressionTranslation 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, parentheses are converted to calling the invoke member function.

Bitwise Operations

Unlike Java, Kotlin does not have bitwise and shift operators. To perform these tasks, various functions (supporting infix notation) are used:

  • shl - Signed Left Shift

  • shr - Signed Right Shift

  • ushr - Unsigned Right Shift

  • and - Bitwise AND

  • or - Bitwise OR

  • xor - Bitwise XOR

  • inv - Bitwise NOT

Visit this page to learn more aboutKotlin Bitwise OperationsMore Information.

Unlike Java, there is no ternary operator in Kotlin.