Kotlin – Operators

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.

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:

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.

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.

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.

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.

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.

``````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.

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 🙂