In this tutorial, you shall learn about Operators in Kotlin, different categories of Operators, and list of operators in each category, with tabular data and examples.
Kotlin Operators
Operators perform operations on operands like variables or values.
In Kotlin, there are five types of operators based on the type of operations they perform. They are
- Kotlin Arithmetic Operators
- Kotlin Assignment Operators
- Kotlin Relational Operators
- Kotlin Logical Operators
- Kotlin Bitwise Operators
Arithmetic Operators
Kotlin Arithmetic Operators perform mathematical arithmetic operations like addition, subtraction, multiplication, etc. The following arithmetic operators are supported in Kotlin.
Arithmetic Operation | Operator Symbol | Example | Description |
---|---|---|---|
Addition | + | a + b | Returns sum of a and b. |
Subtraction | - | a - b | Returns difference of b from a. |
Multiplication | * | a * b | Returns product of a and b. |
Division | / | a / b | Returns the quotient when a is divided by b. |
Modulur | % | a % b | Returns reminder when a is divided by b. |
Increment | ++ | a++ is Post-Increment.++a is Pre-Increment. | Increments the value of a by one. Post-Increment: The value of a is updated, only after the execution moves to next statement.Pre-Increment: The value of a is updated during the execution of this statement itself. |
Decrement | -- | a-- is Post-Decrement.--a is Pre-Decrement. | Decrements the value of a by one. Post-Decrement: The value of a is updated, only after the execution moves to next statement.Pre-Decrement: The value of a is updated during the execution of this statement itself. |
Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Arithmetic Operators in Kotlin.
- Kotlin Addition Operator
- Kotlin Subtraction Operator
- Kotlin Multiplication Operator
- Kotlin Division Operator
- Kotlin Modulus Operator
- Kotlin Increment Operator
- Kotlin Decrement Operator
Assignment Operators
Kotlin Assignment Operators are used to perform an action with given operands and assign the result back to given variable (left operand).
The following table specifies symbol, example, and description for each of the Assignment Operator in Kotlin.
Assignment Operation | Operator Symbol | Example | Description |
---|---|---|---|
Simple Assignment | = | x = 2 | Assign x with 2. |
Addition Assignment | += | x += 3 | Add 3 to the value of x and assign the result to x. |
Subtraction Assignment | -= | x -= 3 | Subtract 3 from x and assign the result to x. |
Multiplication Assignment | *= | x *= 3 | Multiply x with 3 and assign the result to x. |
Division Assignment | /= | x /= 3 | Divide x with 3 and assign the quotient to x. |
Remainder Assignment | %= | x %= 3 | Divide x with 3 and assign the remainder to x. |
Bitwise AND Assignment | &= | x &= 3 | Perform x & 3 and assign the result to x. |
Bitwise OR Assignment | |= | x |= 3 | Perform x | 3 and assign the result to x. |
Bitwise-exclusive-OR Assignment | ^= | x ^= 3 | Perform x ^ 3 and assign the result to x. |
Left-shift Assignment | <<= | x <<= 3 | Left-shift the value of x by 3 places and assign the result to x. |
Right-shift Assignment | >>= | x >>= 3 | Right-shift the value of x by 3 places and assign the result to x. |
Assignment Operators Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Assignment Operators in Kotlin.
- Kotlin Simple Assignment (=) Operator
- Kotlin Addition Assignment Operator
- Kotlin Subtraction Assignment Operator
- Kotlin Multiplication Assignment Operator
- Kotlin Division Assignment Operator
- Kotlin Remainder Assignment Operator
- Kotlin Bitwise AND Assignment Operator
- Kotlin Bitwise OR Assignment Operator
- Kotlin Bitwise XOR Assignment Operator
- Kotlin Left-shift Assignment Operator
- Kotlin Right-shift Assignment Operator
Relational Operators
Relational Operators are used to compare two operands if they are equal, greater, lesser, etc.
The following table gives more information about these relational operators in Kotlin.
Relational Operation | Operator Symbol | Example | Description |
---|---|---|---|
Equal to | == | a == b | Returns true if a is equal to b , else false. |
Not equal to | != | a != b | Returns true if a is not equal to b , else false. |
Greater than | > | a > b | Returns true if a is greater than b , else false. |
Less than | < | a < b | Returns true if a is less than b , else false. |
Greater than or equal to | >= | a >= b | Returns true if a is greater than or equal to b , else false. |
Less than or equal to | <= | a <= b | Returns true if a is less than or equal to b , else false. |
Tutorials
The following tutorials provide a well detailed explanation and examples for each of the Relational Operators in Kotlin.
- Kotlin Equal To Operator
- Kotlin Not Equal Operator
- Kotlin Greater Than Operator
- Kotlin Less Than Operator
- Kotlin Greater Than or Equal To Operator
- Kotlin Less Than or Equal To Operator
Logical Operators
Logical Operators are used to create boolean conditions, modify a boolean expression, or combine two or more simple conditions to form a complex condition.
Kotlin supports the following Logical Operators.
Logical Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | && | a && b | Returns logical AND of a and b . |
OR | || | a || b | Returns logical OR of a and b . |
NOT | ! | !a | Returns logical Not of a . |
Tutorials
The following tutorials cover each of these Logical Operators in detail.
Bitwise Operators
Kotlin Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.
The following table specifies symbol, example, and description for each of the Assignment Operator in Kotlin.
Bitwise Operation | Operator Symbol | Example | Description |
---|---|---|---|
AND | & | x & y | Returns the bitwise AND operation between x and y. |
OR | | | x | y | Returns the bitwise OR operation between x and y. |
XOR | ^ | x ^ y | Returns the bitwise XOR operation between x and y. |
Complement | ~ | ~x | Returns the complement of x. |
Left Shift | << | x << y | Returns the result of x left shifted by y number of places. |
Right Shift | >> | x >> y | Returns the result of x right shifted by y number of places. |
Tutorials
The following tutorials cover each of these Bitwise Operators in detail.
- Kotlin Bitwise AND Operator
- Kotlin Bitwise OR Operator
- Kotlin Bitwise XOR Operator
- Kotlin Bitwise Complement Operator
- Kotlin Bitwise Left-shift Operator
- Kotlin Bitwise Right-shift Operator
Conclusion
In this Kotlin Tutorial, we learned about different operators in Kotlin, with separate tutorial for each of the operator.