Kotlin Operator

Operators are special characters which perform operation on operands (values or variable).There are various kind of operators available in Kotlin.

  1) Arithmetic operator
2) Relation operator
3) Assignment operator
4) Arithmetic Assignment operator
5) Bitwise operation
6) Logical operator


Arithmetic Operator

Arithmetic operators are used to perform basic mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/) etc.

Operator Description Expression Translate to
+ Addition a+b a.plus(b)
- Substraction a-b a.minus(b)
* Multiplication a*b a.times(b)
/ Division a/b a.div(b)
% Modulus a%b a.rem(b)

Example of Arithmetic Operator


fun main(array: Array<String>) {

    //example of arithmetic operator

    var a: Int = 50

    var b: Int = 6

    var c: Int = a + b

    var d: Int = a - b

    var e: Int = a * b

    var f: Int = a % b

    var g: Int = a / b

    println("Addition : ${c}")
    println("Subtraction : ${d}")
    println("Multiplication : ${e}")
    println("Division : ${g}")
    println("Mod : ${f}")

    //it can also be done ...
    println("-----------------");

    var h = a.plus(b)

    var i = a.minus(b)

    var j = a.times(b)

    var k = a.div(b)

    var l = a.rem(b)

    println("Addition : ${h}")
    println("Subtraction : ${i}")
    println("Multiplication : ${j}")
    println("Division : ${k}")
    println("Mod : ${l}")


}
Addition : 56
Subtraction : 44
Multiplication : 300
Division : 8
Mod : 2
-----------------
Addition : 56
Subtraction : 44
Multiplication : 300
Division : 8
Mod : 2

Relation Operator

Relation operator shows the relation and compares between operands. Basically it is used to compare to operands .Following are the different relational operators:

Operator Description Expression Translate to
> greater than x > y a.compateTo(b)>0
< Less than x < y a.compateTo(b)<0
>= greater than or equal to x >= y a.compateTo(b)>=0
<= less than or equal to x <= y a?.equals(b)?:(b===null)
== is equal to x == y a?.equals(b)?:(b===null)
!= not equal to x != y !(a?.equals(b)?:(b===null))

Example of Relational Operator


fun main(array: Array<String>) {
    //example of relational operator

    var a: Int = 4

    var b: Int = 5

    println(a > b)
    println(a.compareTo(b) > 0)

    println(a == b)
    println(a.compareTo(b) == 0)
}
false
false
false
false

Assignment operator

= is assignment operator which is used to assign the value to variable.

fun main(array: Array<String>) {
    //example of assignment operator

    var name:String? = "Durgesh Kumar Tiwari"

    println("My name is ${name}")
}
My name is Durgesh Kumar Tiwari

Arithmetic Assignment operator

wherever Arithmetic and Assignment operator is used together for operation and assignment then these operator as known as Arithmetic Assignment Operator.
for example

+= , -= ,*= , /=  etc

Example of Relational Operator


fun main(array: Array<String>) {
    
    //example of Arithmetic Assignment  operator
    var a: Int = 5

    var b: Int = 6

    a += b

    println("value of a is ${a}")
}
value of a is 11

Logical Operator

Logical operators are used to check conditions between operands. List of logical operators are given below.

Operator Description Expression Convert to
&& return true if all expression are true (x > y) && (x > y) (x > y) and (x > y)
|| return true if any expression are true (x > y) || (x > y) (x > y) or (x > y)
! return complement of expression !x x.not()

Example of Logical Operator

fun main(array: Array<String>) {

    var flag: Boolean = true

    var flag1: Boolean = false


    var flag2: Boolean = true


    println(flag and flag1)

    println(flag or flag1)

    println(flag2.not())
}
false
true
false

Bitwise Operation

In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named function.

    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())  
      
    }  
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