![]() |
VOOZH | about |
Operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.
Example:
Output:
30Explanation: Here, ‘+‘ is an addition operator that adds 10 and 20 operands and returns the value 30 as a result.
Kotlin operators are classified into 6 types based on the type of operation they perform:
Table of Content
Arithmetic operators are used to perform arithmetic or mathematical operations on the operands. For example, ‘+‘ is used for addition.
| Operators | Meaning | Expression | Translate to |
|---|---|---|---|
| + | Addition | a + b | a.plus(b) |
| - | Subtraction | a - b | a.minus(b) |
| * | Multiplication | a * b | a.times(b) |
| / | Division | a / b | a.div(b) |
| % | Modulus | a % b | a.rem(b) |
Example:
Output:
a + b = 24
a - b = 16
a * b = 80
a / b = 5
a % b = 0
Relational operators are used to compare the values of two operands. For example, ‘>’ checks whether the right operand is greater.
| Operators | Meaning | Expression | Translate to |
|---|---|---|---|
| > | greater than | a > b | a.compareTo(b) > 0 |
| < | less than | a < b | a.compareTo(b) < 0 |
| >= | greater than or equal to | a >= b | a.compareTo(b) >= 0 |
| <= | less than or equal to | a <= b | a.compareTo(b) <= 0 |
| == | is equal to | a == b | a?.equals(b) ?: (b === null) |
| != | not equal to | a != b | !(a?.equals(b) ?: (b === null)) > 0 |
Example:
Output:
c > d = false
c < d = true
c >= d = false
c <= d = true
c == d = false
c != d = true
Assignment operators are used to assign a value to a variable. We assign the value of the right operand to left operand according to which assignment operator we use.
| Operators | Expression | Translate to |
|---|---|---|
= | a = 5 | a.equalto(5) |
| += | a = a + b | a.plusAssign(b) > 0 |
| -= | a = a - b | a.minusAssign(b) < 0 |
| *= | a = a * b | a.timesAssign(b)>= 0 |
| /= | a = a / b | a.divAssign(b) <= 0 |
| %= | a = a % b | a.remAssign(b) |
Example:
Output:
15
10
50
10
0
Unary Operators are used to increment or decrement a value.
| Operators | Expression | Translate to |
|---|---|---|
| ++ | ++a or a++ | a.inc() |
| -- | --a or a-- | a.dec() |
Example:
Output:
First print then increment: 10
First increment then print: 12
First print then decrement: 12
First decrement then print: 10
Logical operators are used to combine two or more conditions or constraints, or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.
| Operators | Meaning | Expression |
|---|---|---|
| && | Return true if all expressions are true | (a>b) && (a>c) |
| || | Return true if any of the expressions is true | (a>b) || (a>c) |
| ! | Return the complement of the expression | a.not() |
Example:
Output:
100
25
Logical operators
Bitwise operators work on bit-level. So, compiler first converted to bit-level and then the calculation is performed on the operands.
| Operators | Meaning | Expression |
|---|---|---|
| shl | signed shift left | a.shl(b) |
| shr | signed shift right | a.shr(b) |
| ushr | unsigned shift right | a.ushr() |
| and | bitwise and | a.and(b) |
| or | bitwise or | a.or() |
| xor | bitwise xor | a.xor() |
| inv | bitwise inverse | a.inv() |
Example:
Output:
5 signed shift left by 1 bit: 10
10 signed shift right by 2 bits: : 2
12 unsigned shift right by 2 bits: 3
36 bitwise and 22: 4
36 bitwise or 22: 54
36 bitwise xor 22: 50
14 bitwise inverse is: -15