English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
The following is a list of arithmetic operators in Kotlin:
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 |
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.
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
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:
operator | Function name | Usage |
---|---|---|
a + b | Addition | a.plus(b) |
a-b | Subtraction | a.minus(b) |
a * b | Multiplication | a.times(b) |
a / b | Division | a.div(b) |
a % b | Modulus | a.mod(b) |
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:
operator | is equivalent to | Usage |
---|---|---|
a + assign b | a = a + b | a.plusAssign(b) |
a-assign b | a = a-b | a.minusAssign(b) |
a * assign b | a = a * b | a.timesAssign(b) |
a / assign b | a = a / b | a.assign(b) |
a%= b | a = a%b | a.modAssign(b) |
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
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. | Description | operator | Usage |
---|---|---|---|
+ | Unary plus | +a | a.unaryPlus() |
- | Unary minus | -a | a.unaryMinus() |
! | Not (reverses the value) | !a | a.not() |
++ | Increment: Value plus1 | ++a | a.inc() |
-- | Decrement: Value minus1 | --a | a.dec() |
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
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. | Description | operator | Usage |
---|---|---|---|
> | Greater than | a> b | a.compareTo(b)> 0 |
< | Less than | a <b | a.compareTo(b)<0 |
> = | Greater than or equal to | a> = b | a.compareTo(b)>= 0 |
<= | Less than or equal to | a <= b | a.compareTo(b)<= 0 |
== | Equal | a == b | a?.equals(b)?:(b === null) |
!= | Not equal | a!= b | !(a?.equals(b)?:(b ===null)) |
Comparison and equality operators are used for control flow, such as:if expression,when expression and loop.
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
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. | Description | operator | Corresponding 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.
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)
in operator
The in operator is used to check if an object belongs to a collection. | operator | Usage |
---|---|---|
expression | in | a in b |
b.contains(a) | !in | a !in b |
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.
expression | Usage |
---|---|
a[i] | a.get(i) |
a[i, n] | a.get(i, n) |
a[i1, i2, ..., in] | a.get(i1, i2, ..., in) |
a[i] = b | a.set(i, b) |
a[i, n] = b | a.set(i, n, b) |
a[i1, i2, ..., in] = b | a.set(i1, i2, ..., in, b) |
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
The following are some expressions using the invoke operator with corresponding functions in Kotlin.
expression | Translation to |
---|---|
a() | a.invoke() |
a(i) | a.invoke(i) |
a(i1, i2, ..., in) | a.inkove(i1, i2, ..., in) |
a[i] = b | a.set(i, b) |
In Kotlin, parentheses are converted to calling the invoke member function.
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.