Operators in Kotlin

By | April 17, 2020

In this tutorial, we will learn about Operators in Kotlin. Operators are special symbols in Kotlin and we use operators to perform some special operators on operands. For example, the addition of two variables. In kotlin operators are categorised in various categories.

Types of Operators:

  • Arithmetic operators
  • Relational operators
  • Assignment operators
  • Unary operators
  • Bitwise operations
  • Logical operators

Arithmetic Operators:

Arithmetic Operators are used to performing simple mathematical operations like addition, subtraction, multiplication, division etc.

Operator SymbolOperator NameExample

Example Program:

fun main(args : Array<String>) {  
var num1=20;  
var num2=10;  

Relational operators:

Relational Operators are used to checking the relation between the two operands. For example to check both operands are equal (num1==num2).

Operator SymbolOperator NameExample
>greater thana>b
<Less thana<b
>=greater than or equal toa>=b
<=less than or equal toa<=b
==is equal toa==b
!=not equal toa!=b

Example Program:

fun main(args : Array<String>) {  
    val num1 = 20  
    val num2 = 10  
    if (num1 > num2) {  
        println("num1 is greater")   
    } else{  
        println("num2 is greater")   

Assignment operators:

Assignment Operators are used to assigning value to a variable. We mainly use = operator as assignment operator but we have several other assignment operators in Kotlin.

Operator SymbolOperator NameExample
=equal operatora=b
+=add and assigna+=b
-=subtract and assigna-=b
*=multiply and assigna*=b
/=divide and assigna/=b
%=mod and assigna%=b

Example Program:

fun main(args : Array<String>) {  
    var num1 =20
    var num2=10
    num1 +=num2  
    println("num1+=num2 :"+ num1)  
    num1 -=num2  
    println("num1-=num2 :"+ num1)  
    num1 *=num2  
    println("num1*=num2 :"+ num1)  
    num1 /=num2  
    println("num1/=num2 :"+ num1)  
    num1 %=num2  
    println("num1%=num2 :"+ num1)  

Unary operators:

Unary Operators works on a single operand and there is total of five Unary operators available in Kotlin.

Operator SymbolOperator NameExample
+unary plus+a
unary minus-a
++increment by 1++a
decrement by 1–a

Example Program:

fun main(args: Array<String>){  
    var num1=20  
    var num2=10  
    var light = true  
    println("+num1 :"+ +a)  
    println("-num2 :"+ -b)  
    println("++num1 :"+ ++a)  
    println("--num2 :"+ --b)  
    println("!light :"+ !flag)  

Bitwise operations:

Bitwise operators works at the bit level of operand. But Bitwise operators are not available in Kotlin. There are some predefined functions available in Kotlin which helps us to perform Bitwise operations.

shl (bits)signed shift lefta.shl(b)
shr (bits)signed shift righta.shr(b)
ushr (bits)unsigned shift righta.ushr(b)
and (bits)bitwise anda.and(b)
or (bits)bitwise ora.or(b)
xor (bits)bitwise xora.xor(b)
inv()bitwise inversea.inv()

Example Program:

fun main(args: Array<String>){  
    var num1=20  
    var num2=2  
    println("Signed shift left: "+num1.shl(num2))  
    println("Signed shift right: "+num1.shr(num2))  
    println("Unsigned shift right: "+num1.ushr(num2))  
    println("Bitwise and: "+num1.and(num2))  
    println("Bitwise or: "+num1.or(num2))  
    println("Bitwise xor: "+num1.xor(num2))  
    println("Bitwise inverse: "+num1.inv())  

Logical operators:

Logical operators are used to check Logical relation between Operands. The result of these operators comes in Boolean form true/false. There are three Logical Operators in Kotlin.

Operator SymbolOperator NameExample
&&Logical AND(a>b) && (a>c)
||Logical OR(a>b) || (a>c)
!Logical NOT!a

Example Program:

fun main(args: Array<String>){  
    var num1=20  
    var num2=15  
    var num3=10  
    var light = true  
    var result: Boolean  
    result = (num1>num2) && (num1>num3)  
    println("Logical AND :"+ result)  
    result = (num1>num2) || (num1>num3)  
    println("Logical OR :"+ result)  
    result = !light  
    println("Logical NOT :"+ result)