Kotlin Operator

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

Arithmetic Operator

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

OperatorDescriptionExpressionTranslate to
+Additiona+ba.plus(b)
Subtractiona-ba.minus(b)
*Multiplya*ba.times(b)
/Divisiona/ba.div(b)
%Modulusa%ba.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:

OperatorDescriptionExpressionTranslate to
>greater thana>ba.compateTo(b)>0
<Less thana<ba.compateTo(b)<0
>=greater than or equal toa>=ba.compateTo(b)>=0
<=less than or equal toa<=ba?.equals(b)?:(b===null)
==is equal toa==ba?.equals(b)?:(b===null)
!=not equal toa!=b!(a?.equals(b)?:(b===null))

Example of Relation Operator

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.

    OperatorDescriptionExpressionConvert to
    +=add and assigna+=ba.plusAssign(b)
    -=subtract and assigna-=ba.minusAssign(b)
    *=multiply and assigna*=ba.timesAssign(b)
    /=divide and assigna/=ba.divAssign(b)
    %=mod and assigna%=ba.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.

      OperatorDescriptionExpressionConvert to
      +unary plus+aa.unaryPlus()
      unary minus-aa.unaryMinus()
      ++increment by 1++aa.inc()
      decrement by 1–aa.dec()
      !not!aa.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.

        OperatorDescriptionExpressionConvert to
        &&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!aa.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.

          Named FunctionDescriptionExpression
          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 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
            

            Comments

            Leave a Reply

            Your email address will not be published. Required fields are marked *