Kotlin Operators

8. Kotlin Operators 8.1 Arithmetic Operators 8.2 Assignment Operators 8.3 Logical Operators 8.4 Unary Operators 8.5 Comparison Operators 8.6 in Operators 8.7 Bitwise Operators

Like other programming languages, kotlin also support rich set of operators. Those are as follows -

Arithmetic Operators

Arithmetic operators work on only two numeric operands, if the operands are not numeric then it automatically converts to numeric.

Operator Name Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a%b

Assignment Operators

Assignment operators are used to assign a value to a variable.

Operator Example Description
= a = 2; It assigns the right operand value to the left operand.
+= a += 2; // same as $a = $a+2; It adds both operands and assigns the result to the first operand.
-= a -= 2; // same as $a = $a-2; It subtracts both operands and assigns the result to the first operand.
*= a *= 2; // same as $a = $a*2; It multiplies both operands and assigns the result to the first operand.
/= a /= 2; // same as $a = $a/2; It divides left operand to the right and assigns the result to the first operand.
%= a %= 2; // same as $a = $a%2; It takes modules and assigns the result to the first operand.




Logical Operators

These operators are used to perform logical operations. It returns the result in always a boolean value.

Operator Name Example
&& AND $var && $var2; // Logical AND between $var and $var2
|| OR $var || $var4; // Logical OR between $var and $var4

Unary Operators

Unary Operators take only one value.

Operator Name Example
+ Unary plus a+ // post increment a
+a // pre increment a
- Unary minus a- // post decrement a value
-a // pre decrement a value
++ Increment $a++ // post increment $a
++$a // pre increment $a
-- Decrement $a-- // post decrement $a value
--$a // pre decrement $a value
! Logical Negation !a // True if a evaluates to False.

Comparison Operators

Comparison operators are used for comparison. Its result is always a boolean value.

Operator Name Example
== Equal to a == b; // check the equality between both $a and $b.
=== Identical to a === b; // if value and datatype of both operands are equal, then it returns true.
!= Not equal to a != b; //If the value of both operands are not equal, then it returns true, otherwise it returns false.
< Less than a < b; //If the value of left operand is less than the right operand, then it returns true.
> Greater than a > b; //If the value of left operand is greater than the right operand, then it returns true.
>= Greater than equal to a >= b; //If the value of left operand is greater than or equal to the right operand, then it returns true.
<= Less than equal to a <= b; //If the value of left operand is less than or equal to the right operand, then it returns true.




in Operators

The in operator is used to check whether an object belongs to a collection.

Operator Example Details
in a in b a contains in b
!in a !in b a not contain in b

Bitwise Operators

The in operator is used to check whether an object belongs to a collection.

Operator Name Details
shl signed shift left a << b
shr signed shift right a >> b
ushr unsigned shift right a >>> b
and bitwise and a and b
or bitwise or a or b
xor bitwise xor a xor b
inv bitwise inversion a inv b