Table of Contents
Operators are special characters which perform operation on operands (values or variable).There are various kind of operators available in Kotlin.
- Arithmetic operator
- Relation operator
- Assignment operator
- Unary operator
- Bitwise operation
- Logical operator
- in Operator
- Index access Operator
- Range Operator
Arithmetic Operator
Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) etc.
Operator | Expression | Translate to |
---|---|---|
+ | a+b | a.plus(b) |
– | a-b | a.minus(b) |
* | a*b | a.times(b) |
/ | a/b | a.div(b) |
% | a%b | a.rem(b) |
Example of Arithmetic Operator
fun main(args : Array<String>) {
var a=10;
var b=5;
println(a+b);
println(a-b);
println(a*b);
println(a/b);
println(a%b);
}
Output :
15
5
50
2
0
Relation Operator
Relation operator shows the relation and compares between operands. Following are the different relational operators:
Operator | Description | Exp. | Translate |
---|---|---|---|
> | 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)) |
Output :
fun main(args : Array<String>) {
val a = 5
val b = 10
val max = if (a > b) {
println("a is greater than b.")
a
} else{
println("b is greater than a.")
b
}
println("max = $max")
}
Output :
b is greater than a.
max = 10
Assignment operator
Assignment operator “=” is used to assign a value to another variable. The assignment of value takes from right to left.
Operator | Description | Exp. | Translate |
---|---|---|---|
+= | 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 of Assignment operator
fun main(args : Array<String>) {
var a =20;var b=5
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 :
a+=b :25
a-=b :20
a*=b :100
a/=b :20
a%=b :0
Unary Operator
Unary operator is used with only single operand. Following are some unary operator given below.
Operator | Description | Exp. | Translate |
---|---|---|---|
+ | 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 of Unary Operator
fun main(args: Array<String>){
var a=10
var b=5
var flag = true
println("+a :"+ +a)
println("-b :"+ -b)
println("++a :"+ ++a)
println("--b :"+ --b)
println("!flag :"+ !flag)
}
Output :
+a :10
-b :-5
++a :11
--b :4
!flag :false
Logical Operator
Logical operators are used to check conditions between operands. List of logical operators are given below.
Operator | Description | Exp. | Translate |
---|---|---|---|
&& | 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 of Logical Operator
fun main(args: Array<String>){
var a=10
var b=5
var c=15
var flag = false
var result: Boolean
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 :
(a>b) && (a>c) :false
(a>b) || (a>c) :true
!flag :true
Bitwise Operation
In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named function.
Function | Expression | Description |
---|---|---|
shl (bits) | a.shl(b) | signed shift left |
shr (bits) | a.shr(b) | signed shift right |
ushr (bits) | a.ushr(b) | unsigned shift right |
and (bits) | a.and(b) | bitwise and |
or (bits) | a.or(b) | bitwise or |
xor (bits) | a.xor(b) | bitwise xor |
inv() | a.inv(b) | bitwise inverse |
Example of Bitwise Operation
fun main(args: Array<String>){
var a=10
var b=2
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 :
a.shl(b): 40
a.shr(b): 2
a.ushr(b:) 2
a.and(b): 2
a.or(b): 10
a.xor(b): 8
a.inv(): -11
in Operator
The in operator is used to check whether an object belongs to a collection.
Operator | Expression | Translate to |
---|
in | a in b | b.contains(a) |
!in | a !in b | !b.contains(a) |
fun main(args: Array<String>) {
val numbers = intArrayOf(1, 4, 42, -3)
if (4 in numbers) {
println("numbers array contains 4.")
}
}
Output :
numbers array contains 4.
Index access Operator
Here are some expressions using index access operator with corresponding functions in Kotlin.
Operator | Translate 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 :
fun main(args: Array<String>) {
val a = intArrayOf(1, 2, 3, 4, - 1)
println(a[1])
a[1]= 12
println(a[1])
}
Output :
2
12
Range Operator
Kotlin range is defined as an interval from start value to the end value. Range expressions are created with operator (. .) which is complemented by in and !in. The value which is equal or greater than start value and smaller or equal to end value comes inside the definedrange.
valaToZ = 'a'..'z'
valoneToNine = 1..9
Example of Kotlin range
fun main(args: Array<String>) {
for (a in 1..5){
print(a )
}
println()
for(x in 'a'..'f'){
print(x )
}
println()
val range = 1.0..5.0
println(range)
println("3.14 in range is ${3.14 in range}")
}
Output:
12345
abcdef
1.0..5.0
3.14 in range is true
I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.
Thanks for reading, have a nice day 🙂