Kotlin Operators

Operators is an mathematical symbols or special characters. which are performed mathematically, logical and assignment operation in programming language. That is divided into 6 category in kotlin programming.

Arithmetic operator

That is perform some basic mathematical operation like plus, minus, divide, multiply and remainder.

Operator Name
+ Addition( plus sign)
- Subtract (minus sign)
* Multiplication (* symbol)
/ Divide
% Remainder operator

For example.

//Example of Arithmetic operator
fun main(args: Array<String>) {
  //add two integers
  println(3+5)
  //divide two double values
  println(17.5-6.5)
  //add one binary and integer values
  println(0b01010 + 23)
  //multiply value of two integers
  println(3*2)
  //divide value of two integers
  println(7/3)
  //modules of two integers
  println(8%3)
   
}

Output

8
11.0
33
6
2
2

Relation operator

Relational operators are used to compare values and expression results.

Operator Name
> Greater than
< Less than
== Equal to equal to
>= Greater than equal to
<= Less than equal to
!= Not equal to

For example

//Example of Relation operator
fun main(args: Array<String>) {
  
  if(6!=9){
    println("6!=9")
  }
  else {
    println("6==9")
  }

  val age:Int=20
  //assign result to variable
  var votingStatus=if(age>=18){
    true
  }else{
    false
  }
  println(votingStatus)

}

Output

6!=9
true

Assignment operator

Assignment operator are work on two operands. there is left operands is an variable. Which is used to stored the resultant of statements.

Operator Meaning
= Equal
*= Multiply and assign
+= Add and assign
/= Divide and assign
-= Subtract and assign
%= Calculate remainder and assign
//Example of Assignment operator
fun main(args: Array<String>) {
  
  var num1:Int=4
  var num2:Int=5
  println("Before num1 = $num1  num2 = $num2")

  num1+=num2 //same as num1=num1+num2

  println("After (num1+=num2) : num1 = $num1  num2 = $num2")

  num1=3
  num2=2

  println("Reset num1 = $num1  num2 = $num2")

  num1-=num2 //same as num1=num1-num2
  println("After (num1-=num2) : num1 = $num1  num2 = $num2")


  num1=4
  num2=7

  println("Reset num1 = $num1  num2 = $num2")

  num2*=num1 //same as num1=num1*num2
  println("After (num2*=num1) : num1 = $num1  num2 = $num2")

  num1=4
  num2=3

  println("Reset num1 = $num1  num2 = $num2")

  num1%=num2 //same as num1=num1%num2
  println("After (num1%=num1) : num1 = $num1  num2 = $num2")

  num1=9
  num2=3

  println("Reset num1 = $num1  num2 = $num2")

  num1/=num2 //same as num1=num1/num2
  println("After (num1/=num1) : num1 = $num1  num2 = $num2")



}

Output

Before num1 = 4  num2 = 5
After (num1+=num2) : num1 = 9  num2 = 5
Reset num1 = 3  num2 = 2
After (num1-=num2) : num1 = 1  num2 = 2
Reset num1 = 4  num2 = 7
After (num2*=num1) : num1 = 4  num2 = 28
Reset num1 = 4  num2 = 3
After (num1%=num1) : num1 = 1  num2 = 3
Reset num1 = 9  num2 = 3
After (num1/=num1) : num1 = 3  num2 = 3

Bitwise operation

Operator Meaning
and Bitwise AND
or Bitwise OR
xor Bitwise XOR
inv() Bitwise complement(Bitwise inverse)
shl(bits) Signed shift left
shr(bits) Signed shift right
ushr(bits) Unsigned shift right

and : Bitwise and operators is working on logical value. When this operator are applied on two operands then this are work on its binary value. When there result are (1 and 1) their resultant is 1 (true). Otherwise 0.

//Example of Bitwise and
fun main(args: Array<String>) {
  
  var data1:Int=7
  var data2:Int=3
  /*Logical and
  *   7 = 0  1  1  1 (binary of 7)
  *   3 = 0  0  1  1 (binary of 3)
    and   
  --------------------------------
  Result= 0  0  1   1 (binary of 3)
  --------------------------------- 
  Working : 
       1   1  = 1 (Both true then true)
       0   1  = 0 (Any one false then false)

  */

  println("data1.and(data2) = ${data1.and(data2)}")

  //same as 
  println("(data1 and data2) = ${data1 and data2}")
}

Output

data1.and(data2) = 3
(data1 and data2) = 3

bitwise or: When resultant binary is 0 or 0 then resultant is 0 otherwise resultant is 1. See this example.

//Example of Bitwise or
fun main(args: Array<String>) {
  
  var data1:Int=7
  var data2:Int=3
  /*Bitwise or
  *   7 = 0  1  1  1 (binary of 7)
  *   3 = 0  0  1  1 (binary of 3)
    or   
  --------------------------------
  Result= 0  1  1   1 (binary of 3)
  --------------------------------- 
  Working : 
       0   0  = 0 (Both false then false)
       0   1  = 0 (Any one true then true)

  */

  println("data1.or(data2) = ${data1.or(data2)}")

  //Same as 
  println("(data1 or data2) = ${data1 or data2}")
}

Output

data1.or(data2) = 7
(data1 or data2) = 7

bitwise xor: When resultant binary is (1 0) or (0 1) then resultant is 1 otherwise resultant is 0. See this example.

//Example of Bitwise xor
fun main(args: Array<String>) {
  
  var data1:Int=15
  var data2:Int=3
  /*Bitwise xor
  *    15 = 1  1  1  1 (binary of 15)
  *   3 = 0  0  1  1 (binary of 3)
    xor   
  --------------------------------
  Result= 1  1  0  0 (binary of 12)
  --------------------------------- 
  Working : 
       1   0  = 1 (true)
       0   1  = 1 (true)

       Otherwise False
       1   1  =0
       0   0  =0

  */

  println("data1.xor(data2) = ${data1.xor(data2)}")

  //same as 
  println("(data1 xor data2) = ${data1 xor data2}")
}

Output

data1.xor(data2) = 12
(data1 xor data2) = 12

inv: Bitwise inverse example.

//Example of Bitwise inv
fun main(args: Array<String>) {
  
  var data1:Int=15
  var data2:Int=-6
  /*Bitwise inv
    Example 1:

      15  = 1  1  1  1 (binary of 15)
      add            1 (binary of 1)
    +   
  --------------------------------
  Result= 1  1  1  1 (binary of 16)
  --------------------------------- 
    output= - Result
    output= - 16

    Example 2: first remove that (- sign)
      6   = 0  1  1  0 (binary of  6)
      add            1 (binary of  1)
    +   
  --------------------------------
  Result= 0  1  0  1 (binary of 5)
  Result= 5 
  //add remove sign
  Result =-5 
  --------------------------------- 
    //multiply by (- sign)
    output= - Result
    output= - (-5)
    output= 5

  */

  println("data1.inv() = ${data1.inv()}")
  println("data2.inv() = ${data2.inv()}")

}

Output

data1.inv() = -16
data2.inv() = 5

shl: Bitwise signed shift left example.

//Example of Bitwise shl
fun main(args: Array<String>) {
  
  var data1:Int=15
  var data2:Int=2
  /*Bitwise shl
    Example 1:

      15        = 0  0  1  1  1  1 (binary of 15)
  left shift 2  = 1  1  1  1  0  0  
      
  --------------------------------
   60     =     1  1  1  1  0  0 
  ---------------------------------
  Or
  Formula   = data * (2^ left shift)
  example 1 =  15   *(2^2)  2 bit shift
            =  15*4
            =  60
  example 2 =  2 *(2^5) 
        =  2 *(32)
        =  64          
  */

  println("data1.shl(data2) = ${data1.shl(data2)}")
 println("data2.shl(5) = ${data2.shl(5)}")

}

Output

data1.shl(data2) = 60
data2.shl(5) = 64

shr(): Bitwise signed shift right example.

//Example of Bitwise shr
fun main(args: Array<String>) {
  
  var data1:Int=15
  var data2:Int=2
  /*Bitwise shr
    Example 1:

      15        = 1  1  1  1 (binary of 15)
  right shift 2 = 0  0  1  1  
      
  --------------------------------
   3     =        0  0  1   1
  ---------------------------------     
  */

  println("data1.shr(data2) = ${data1.shr(data2)}") //3
  println("data2.shr(1) = ${data2.shr(1)}") //1

}

Output

data1.shr(data2) = 3
data2.shr(1) = 1

ushr(): Bitwise unsigned shift right example.

//Example of Bitwise ushr
fun main(args: Array<String>) {
  
  var data1:Int=8
  var data2:Int=7
  /*Bitwise ushr
    Example 1:

      8         = 1  0  0  0 (binary of 8)
  right shift 3 = 0  0  0  1  
      
  --------------------------------
   1     =      0  0  0   1
  ---------------------------------     
  */

  println("data1.ushr(2) = ${data1.ushr(2)}")
  println("data2.ushr(1) = ${data2.ushr(1)}")

}

Output

data1.ushr(2) = 2
data2.ushr(1) = 3

Logical operator

Operator Name
&& Logical AND operator.
|| Logical OR Operator
! Logical NOT Operator
//Example of Logical operator
fun main(args: Array<String>) {
  
  var data1:Int=8
  var data2:Int=7
  var result:Boolean=true
  println(!result) //false

  //&& when check both condition is true
  println(data1>data2 && result==true)//true

  println(data2>data1 || result==true) //true (because second condition are valid)

}

Output

false
true
true

Unary operator

This type of operators are work on single operand.

Operator Name
- Unary minus
++ Increment
-- Decrement
! not

Unary minus: This operator are modify argument sign of operand.

fun main(args: Array<String>) {
  var a:Int=10
  var b:Int=-20
  print("Before a : $a  b: $b")
  a=-a
  b=-b
  println("\nAfter  a : $a  b: $b")  
}

Output

Before a : 10  b: -20
After  a : -10  b: 20

Increment (++): This are used to increment the variable value by one. This is two variant, prefix increment and postfix increment.

fun main(args: Array<String>) {
  var testOne:Int=10
  var testTwo:Int=10
  var b:Int=testOne++ //postfix increment b=10
  var c:Int=testOne //c=11
  println("number :$testOne")
  println("b  :$b")
  println("c  :$c")
  
  b=++testTwo //prefix increment
  c=testOne  //11
  println("number :$testTwo")
  println("b  :$b")
  println("c  :$c")
}

Output

number :11
b  :10
c  :11
number :11
b  :11
c  :11

prefix increment are first increment variable value, and after that use to variable values. Postfix increment are first use variable value, after that increment the variable value by one.

Decrement (--): This are used to decrement the variable value by one. This is two variant, prefix decrement and postfix decrement.

fun main(args: Array<String>) {
  var info:Int=10
  //prefix decrement
  var  a:Int =--info //9
  var  b:Int =info  //9
  println("info :$info")
  println("a  :$a")
  println("b  :$b")
  
  info=10
  //posfix decrement
  a=info-- //10
  b=info  //9
  println("info :$info")
  println("a  :$a")
  println("b  :$b")
}

Output

info :9
a  :9
b  :9
info :9
a  :10
b  :9

prefix decrement are first decrement variable value, and after that use to variable values. Postfix decrement are first use variable value, after that decrement the variable value by one.

not(!): This is logical operator, that are used to change boolean expression result value.

fun main(args: Array<String>) {
  if(!true){ 
     print("When Not true")  
  }else{
     print("When true") 
  }
}

Output

When true

Elvis operator kotlin

This operator are check first operands is not null then return first operand. otherwise returning other result.

fun main(args: Array<String>) {
  
  var message:String ?=null
  //if message not null then return message
  //otherwise assign other value
  message=message?: "Learn Kotlin"
  println("Message : $message")

}

Output

Message : Learn Kotlin


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved