Preface
In Kotlin, like many other programming languages, there is concept of operators and Operands. An operand is typically a value or variable to which an operator is applied to perform a specific operation.
In the example below, the numbers 100 and 50 are operands and the plus sign + is an operator.
operand | Operator | operand |
---|---|---|
10 | + | 5 |
There are different types of operators:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- Index operators
Arithmetic operators
These make it possible to carry out simple mathematical calculations, such as: Addition (+), Subtraction (-), multiplication (*), division (/) and more.
Special feature of division: If both operands in a division are integers, the result is also an integer. However, if a fraction is created in the division process, it is cut off. For example: If 22 is divided by 5, according to the rules of mathematics, this equals 4,4. However, since both operands are integers (Int type), the fractional 0,4 is truncated and the result is 4, while the variable c represents Int type.
If you want a result as a decimal number, one of the two operands must be a floating point number.
Other arithmetic operators also include Modulo(%), increment(++) and decrement(–).
% (modulo operator) calculates the remainder of a division. For example, if you calculate 7% 3, you get 1 because the remainder of dividing 7 by 3 is 1.
++ (increment operator) increases the value of a variable by 1. For example, if you have a variable a with the value 10 have and ++ a you write, the value of a on 11 elevated.
— (decrement operator) decreases the value of a variable by 1. For example, if you have a variable a with the value 10 have and -A you write, the value of a on 9 reduced.
It is important to note that the position of the increment or decrement operator affects the value that the variable has after the operation. If you for example a ++ you write, the value of a only increased after the operation.
However, if you ++ a you write, the value of a increased immediately.