Left Index

Kotlin Operators

There are various kind of operators available in Kotlin.

  • Arithmetic operator
  • Relation operator
  • Assignment operator
  • Unary operator
  • Bitwise operation
  • Logical operator

Arithmetic Operator

Arithmetic Operator are used to perform mathematically operation like plus, minus, multiply etc...

Below are list of Arithmetic Operators:

Operator Description Expression Translate To
+ Addtion num1 + num2 num1.plus(num2)
- Substraction num1 - num2 num1.minus(num2)
* Multiply num1 * num2 num1.times(num2)
/ Division num1 / num2 num1.div(num2)
% Modulus num1 % num2 num1.mod(num2) now num1.rem(num2)

Arithmetic Operator Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 41
	var num2 = 2
	
	println("Addition: ${num1.plus(num2)}")
	println("Substraction: ${num1.minus(num2)}")
	println("Multiply: ${num1.times(num2)}")
	println("Division: ${num1.div(num2)}")
	println("Modulus: ${num1.rem(num2)}")
}
Output:
Addition: 43
Substraction: 39
Multiply: 82
Division: 20
Modulus: 1

Relation operator

Relation Operator defines some kind of relation between two entities. These include numerical equality (e.g., 5 == 5) and inequalities (e.g., 4 ≥ 3) etc...

Following are the different relational operators:

Operator Description Expression Translate To
< Less than num1 < num2 num1.compareTo(num2) < 0
> Greater than num1 > num2 num1.compareTo(num2) > 0
<= Less than or equal to num1 <= num2 num1.compareTo(num2) <= 0
>= Greater than or equal to num1 >= num2 num1.compareTo(num2) >= 0
== Is equal to num1 == num2 num1.compareTo(num2) == 0
!= Not equal to num1 != num2 num1.compareTo(num2) != 0

Relation operator example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 41
	var num2 = 2
	
	if(num1.compareTo(num2) > 0) { // or we can use if(num1 > num2)
		println("Num1 is greater than from num2")
	}
	if(num1.compareTo(num2) < 0) { // or we can use if(num1 < num2)
		println("Num1 is less than from num2")
	}
	if(num1.compareTo(num2) >= 0) { // or we can use if(num1 >=num2)
		println("Num1 is greater than or equal to num2")
	}
	if(num1.compareTo(num2) <= 0) { // or we can use if(num1 <= num2)
		println("Num1 is less than or equal to num2")
	}
	if(num1.compareTo(num2) == 0) { // or we can use if(num1 == num2)
		println("Num1 is equal to num2")
	}
	if(num1.compareTo(num2) != 0) { // or we can use if(num1 != num2)
		println("Num1 not equal to num2")
	}
}

Assignment operator

Assignment operator '=' is used to assign a value to another variable. The assignment of value takes from right to left.

Following are the different Assignment operators:

Operator Description Expression
+= Addtion and Assigned num1 += num2
-= Substract and Assigned num1 -= num2
*= Multiply and Assigned num1 *= num2
/= Divide and Assigned num1 /= num2
%= Modulus and Assigned num1 %= num2
Assignment operator example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 41
	var num2 = 2
	
	num1 += num2
	println("Add and Assigned: ${num1}")
	
	num1 -= num2
	println("Substract and Assigned: ${num1}")

	num1 *= num2
	println("Multiply and Assigned: ${num1}")

	num1 /= num2
	println("Divide and Assigned: ${num1}")

	num1 %= num2
	println("Modulus and Assigned: ${num1}")
}

Unary operator

Unary Operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is the function f : A -> A, where A is a set. The function f is a unary operation on A.

Following are some unary operator given below.

Operator Description Expression Translate To
+ Unary Plus +num1 num1.unaryPlus()
- Unary Minus -num1 num1.unaryMinus()
++ Increment by 1 ++num1 num1.inc()
-- Decrement by 1 --num1 num1.dec()
! not !num1

Unary operator example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 41

	println("Unary plus: ${num1.unaryPlus()}")
	println("Unary minus: ${num1.unaryMinus()}")
	println("Unary increment: ${num1.inc()}")
	println("Unary decrement: ${num1.dec()}")
	
	var result = true
	result = !result;
	println("Unary not: ${result}")
}
Output:
Unary plus: 41
Unary minus: -41
Unary increment: 42
Unary decrement: 40
Unary not: false

Bitwise operation

Bitwise Operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast and simple action, directly supported by the processor, and is used to manipulate values for comparisons and calculations.


Note: In Kotlin, there is not any special bitwise operator. Bitwise operation is done using named function as shown in below table:

Function Description
shl(noOfBits) Signed shift left
shr(noOfBits) Signed shift right
ushr(noOfBits) Unsigned shift right
and(noOfBits) bitwise and
or(noOfBits) bitwise or
xor(noOfBits) bitwise xor
inv() bitwise inverse

Bitwise operator example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 10
	
	println("Signed shift left: ${num1.shl(1)}")
	println("Signed shift right: ${num1.shr(1)}")
	println("Unsigned shift right: ${num1.ushr(1)}")
	
	num1 = 10
	var num2 = 20
	println("Bitwise and: ${num1.and(num2)}")
	println("Bitwise or: ${num1.or(num2)}")
	println("Bitwise xor: ${num1.xor(num2)}")
	println("Bitwise inverse: ${num1.inv()}")
}
Output:
Signed shift left: 20
Signed shift right: 5
Unsigned shift right: 5
Bitwise and: 0
Bitwise or: 30
Bitwise xor: 30
Bitwise inverse: -11

Logical operator

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

Operator Description Expression
&& return true if both expression true num1 == 10 && num2 == 20
|| return true if any one expression true num1 == 10 || num2 == 20

Logical Operator example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var num1 = 10
	var num2 = 20
	
	if(num1 == 10 && num2 == 20) {
		println("Both expression are true")
	}

	num2 = 30
	if(num1 == 10 || num2 == 20) {
		println("Any one expression is true")
	}	
}
Output:
Both expression are true
Any one expression is true