In this tutorial you will learn about the Kotlin Operators and its application with practical example.
Kotlin Operators
An operator is a special symbol that is used to carry out some specific operation on its operand. In Kotlin, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Kotlin operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.
Type of operators in Kotlin
Kotlin supports the following types of operators –
- Arithmetic Operators
- Assignment Operators
- Comparison (Relational) Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
Arithmetic Operators In Kotlin
Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc.
Operator | Description | Expression | Translate to |
---|---|---|---|
+ |
Addition |
a+b |
a.plus(b) |
- |
Subtraction |
a-b |
a.minus(b) |
* |
Multiply |
a*b |
a.times(b) |
/ |
Division |
a/b |
a.div(b) |
% |
Modulus |
a%b |
a.rem(b) |
Example:-
1 2 3 4 5 6 7 8 9 10 |
fun main(args : Array<String>) { var a=20; var b=10; println("W3Adda - Arithmetic Operators") println(a+b); println(a-b); println(a*b); println(a/b); println(a%b); } |
Output:-
1 2 3 4 5 6 |
W3Adda - Arithmetic Operators 30 10 200 2 0 |
Assignment Operators In Kotlin
Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.
Operator | Description | Expression | Convert to |
---|---|---|---|
+= |
add and assign |
a+=b |
a.plusAssign(b) |
-= |
subtract and assign |
a-=b |
a.minusAssign(b) |
*= |
multiply and assign |
a*=b |
a.timesAssign(b) |
/= |
divide and assign |
a/=b |
a.divAssign(b) |
%= |
mod and assign |
a%=b |
a.remAssign(b) |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun main(args : Array<String>) { var a =30;var b=5 println("W3Adda - Assignment Operators") a+=b println("a+=b :"+ a) a-=b println("a-=b :"+ a) a*=b println("a*=b :"+ a) a/=b println("a/=b :"+ a) a%=b println("a%=b :"+ a) } |
Output:-
1 2 3 4 5 6 |
W3Adda - Assignment Operators a+=b :35 a-=b :30 a*=b :150 a/=b :30 a%=b :0 |
Comparison (Relational) Operators In Kotlin
Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.
Here is full list of Relational operators available in Kotlin –
Operator | Description | Expression | Translate to |
---|---|---|---|
> |
greater than |
a>b |
a.compateTo(b)>0 |
< |
Less than |
a<b |
a.compateTo(b)<0 |
>= |
greater than or equal to |
a>=b |
a.compateTo(b)>=0 |
<= |
less than or equal to |
a<=b |
a?.equals(b)?:(b===null) |
== |
is equal to |
a==b |
a?.equals(b)?:(b===null) |
!= |
not equal to |
a!=b |
!(a?.equals(b)?:(b===null)) |
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
fun main(args : Array<String>) { val a = 20 val b = 10 val max = if (a > b) { println("W3Adda - Relational Operators") println("a is greater than b.") } else{ println("W3Adda - Relational Operators") println("b is greater than a.") } } |
Output:-
1 2 |
W3Adda - Relational Operators a is greater than b. |
Kotlin Logical Operators
Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.
There are 3 logical operators available in Kotlin.
Operator | Description | Expression | Convert to |
---|---|---|---|
&& |
return true if all expression are true |
(a>b) && (a>c) |
(a>b) and (a>c) |
|| |
return true if any expression are true |
(a>b) || (a>c) |
(a>b) or(a>c) |
! |
return complement of expression |
!a |
a.not() |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun main(args: Array<String>){ var a=20 var b=10 var c=25 var flag = false var result: Boolean println("W3Adda - Logical Operators") result = (a>b) && (a>c) println("(a>b) && (a>c) :"+ result) result = (a>b) || (a>c) println("(a>b) || (a>c) :"+ result) result = !flag println("!flag :"+ result) } |
Output:-
1 2 3 4 |
W3Adda - Logical Operators (a>b) && (a>c) :false (a>b) || (a>c) :true !flag :true |
Unary Operators In Kotlin
The unary operators operate on a single operand, here is full list of kotlin unary operators.
Operator | Description | Expression | Convert to |
---|---|---|---|
+ |
unary plus |
+a |
a.unaryPlus() |
- |
unary minus |
-a |
a.unaryMinus() |
++ |
increment by 1 |
++a |
a.inc() |
-- |
decrement by 1 |
--a |
a.dec() |
! |
not |
!a |
a.not() |
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
fun main(args: Array<String>){ var a=20 var b=10 var flag = true println("W3Adda - Unary Operators") println("+a :"+ +a) println("-b :"+ -b) println("++a :"+ ++a) println("--b :"+ --b) println("!flag :"+ !flag) } |
Output:-
1 2 3 4 5 6 |
W3Adda - Unary Operators +a :20 -b :-10 ++a :21 --b :9 !flag :false |
Bitwise operators in Kotlin
Bitwise operator are used to perform bit level operation over its operand.
Named Function | Description | Expression |
---|---|---|
shl (bits) |
signed shift left |
a.shl(b) |
shr (bits) |
signed shift right |
a.shr(b) |
ushr (bits) |
unsigned shift right |
a.ushr(b) |
and (bits) |
bitwise and |
a.and(b) |
or (bits) |
bitwise or |
a.or(b) |
xor (bits) |
bitwise xor |
a.xor(b) |
inv() |
bitwise inverse |
a.inv() |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun main(args: Array<String>){ var a=20 var b=4 println("W3Adda - BitWise Operators") println("a.shl(b): "+a.shl(b)) println("a.shr(b): "+a.shr(b)) println("a.ushr(b:) "+a.ushr(b)) println("a.and(b): "+a.and(b)) println("a.or(b): "+a.or(b)) println("a.xor(b): "+a.xor(b)) println("a.inv(): "+a.inv()) } |
Output:-
1 2 3 4 5 6 7 8 |
W3Adda - BitWise Operators a.shl(b): 320 a.shr(b): 1 a.ushr(b:) 1 a.and(b): 4 a.or(b): 20 a.xor(b): 16 a.inv(): -21 |
Kotlin in Operator
Kotlin in operator allows you to check whether an object belongs to a collection or not.
Operator | Expression | Translates to |
---|---|---|
in |
a in b |
b.contains(a) |
!in |
a !in b |
!b.contains(a) |
Example:-
1 2 3 4 5 6 7 8 9 |
fun main(args: Array<String>) { val nums = intArrayOf(1, 4, 6, 8, 12, 15) if (6 in nums) { println("W3Adda - in Operators") println("number 6 exists in nums array") } } |
Output:-
1 2 |
W3Adda - in Operators number 6 exists in nums array |
Index access Operator
Index access operator allows you to access elements with specified index in an array or collection.
Expression | Translated to |
---|---|
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) |
Example:-
1 2 3 4 5 6 7 |
fun main(args: Array<String>) { val nums = intArrayOf(1, 4, 6, 8, 12, 15) println("W3Adda - Index Access Operators") println(nums[1]) nums[1]= 2 println(nums[1]) } |
Output:-
1 2 3 |
W3Adda - Index Access Operators 4 2 |
Kotlin Invoke Operator
Expression | Translated 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) |