Javatpoint Logo
Javatpoint Logo

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.

Operator Description Expression Translate to
+ Addition a+b
- Subtraction a-b a.minus(b)
* Multiply a*b a.times(b)
/ Division a/b a.div(b)
% Modulus a%b a.rem(b)

Example of Arithmetic Operator



Relation Operator

Relation operator shows the relation and compares between operands. Following are the different relational operators:

Operator Description Expression Translate to
> greater than a>b a.compateTo(b)>0
< Less than a<b a.compateTo(b)<0
>= greater than or equal to a>=b a.compateTo(b)>=0
<= less than or equal to a<=b a?.equals(b)?:(b===null)
== is equal to a==b a?.equals(b)?:(b===null)
!= not equal to a!=b !(a?.equals(b)?:(b===null))

Example of Relation Operator


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.

Operator Description Expression Convert to
+= add and assign a+=b a.plusAssign(b)
-= subtract and assign a-=b a.minusAssign(b)
*= multiply and assign a*=b a.timesAssign(b)
/= divide and assign a/=b a.divAssign(b)
%= mod and assign a%=b a.remAssign(b)

Example of Assignment operator


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.

Operator Description Expression Convert to
+ unary plus +a a.unaryPlus()
- unary minus -a a.unaryMinus()
++ increment by 1 ++a
-- decrement by 1 --a a.dec()
! not !a a.not()

Example of Unary Operator


+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.

Operator Description Expression Convert 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 !a a.not()

Example of Logical Operator


(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 Function 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 of Bitwise Operation


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

Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials

B.Tech / MCA