Operators in Kotlin

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 Symbol Operator Name Example
+ Addition a+b
Subtraction a-b
* Multiply a*b
/ Division a/b
% Modulus a%b

Example Program:

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

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 Symbol Operator Name Example
> greater than a>b
< Less than a<b
>= greater than or equal to a>=b
<= less than or equal to a<=b
== is equal to a==b
!= not equal to a!=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 Symbol Operator Name Example
= equal operator a=b
+= add and assign a+=b
-= subtract and assign a-=b
*= multiply and assign a*=b
/= divide and assign a/=b
%= mod and assign a%=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 Symbol Operator Name Example
+ unary plus +a
unary minus -a
++ increment by 1 ++a
decrement by 1 –a
! not !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.

Function Name/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 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 Symbol Operator Name Example
&& 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)  
}

 

Spread the love
Scroll to Top