Instruction 1
Operators and Operations
Operators are symbols that enable you to perform specific actions on data. Data types need specific operations for their particular type. You can’t multiply true and false, for instance. Data operations are time-consuming for people to calculate. But computers are excellent at performing them. As you continue to learn, you’ll see the speed of these calculations. Kotlin supports many different types of operators and operations by default. The data involved in an operation is an operand.
Operators are classified by how many operands or data they operate on. These single operand operators are known as unary operators. Most operators act on two operands: the operand on the left-hand side of the evaluation and the operand on the right-hand side of the evaluation. Kotlin’s built-in operators may have a corresponding function that behaves just as the operator symbols. Visit Kotlin Playground to start a new session and take a look at the various types of operators Kotlin supports.
Using Arithmetic Operators
Arithmetic or mathematical operators perform basic mathematical operations on data. These operators operate on numerical types. The same operators may be used with other data types. For instance, adding strings: “Kod” + “eco” = “Kodeco”. For strings, the implementation is different, but the result has a similar meaning. Basic mathematical operations supported in Kotlin are:
Addition
This operator performs a sum of the operands. + is the addition operator:
fun main() {
println(12 + 4) // Two raw values as operands
val left = 12
println(left + 4) // A variable and a raw value as operands
val right = 4
println(left + right) // Two variables as operands
}
Run the code. The result is the same for all three operations.
16
16
16
If the operands are of different types, the type for the result is the type of the operand with the highest precision. Also, not all Number types can be added.
Subtraction
This performs a deduction of the value on the right-hand side of the operand from the value on the left-hand side of the operand. Like the addition operator above, it works with both raw values and variables. You use the - symbol for this operator:
fun main() {
val left = 12
println(left - 4) // A variable and a raw value as operands
}
Run the code. The result was 8, just as expected.
8
For operands of different types, the type for the result is the type for the operand with the highest precision. Not all number types can be subtracted from each other.
Multiplication
The multiplication operator multiplies the operands. This operator is implemented by the * symbol:
fun main() {
val left = 12
println(left * 4) // A variable and a raw value as operands
}
Run the code. Check the console for the result:
48
The resulting type for the value returned after the operation will be the type for the operand with the highest precision. Also, not all Number types can be multiplied by each other.
Division
The division operator divides the left operand by the right operand. The forward slash / is the symbol for the division operator. Just as division by 0 is mathematically incorrect, dividing by 0 will result in an error for integers and Infinity for floats and doubles.
fun main() {
val left = 12
println(left / 4) // A variable and a raw value as operands
}
Run the code. In the console, the result is:
3
Not all Number types can be operands for the division operator.
Note: Dividing two integers results in an integer, even if both operands are not divisible. Thus,
3/2will give you1, although you know it’s actually1.5, i.e. a floating point result. Read on to know how to get the remainder of an integer’s division.
Modulus
This operator returns the remainder after performing a division between the operands. It’s represented by the symbol %:
fun main() {
val left = 12
println(left % 4) // A variable and a raw value as operands
}
Run the code. In the console, the result is:
0
There are two other unary operators that Kotlin supports. Read on to find out more.
Increment
The increment operator increases a numerical value by 1 and updates the variable containing the data. It’s in the form of ++. It doesn’t work on raw values, variables, or objects only. It doesn’t work for all numerical types. The variable must be mutable since it’ll be updated at the end of the operation:
fun main() {
var count = 12
println(count++)
}
Run the code. This prints 12. It doesn’t mean count was not incremented. It was incremented after the print statement was executed. This is because the increment operator appears after the variable.
Note: The Kotlin Playground IDE shows a circled letter ‘i’ below. This graphic is the IDE’s way of letting you know that you need to double-check some aspects of your code. The code will run successfully, but the IDE points out that the output of the operation
count++is never used. The information is the IDE’s way of helping you with your code. The Android Studio IDE will have a similar feature for pointing out helpful observations about your code. Hover your mouse over the ‘i’ and you will see the following hint:
The next example prints the value of count before and after the increment operation:
fun main() {
var count = 12
println(count++) // Prints 12
println(count) // Prints 13
}
Run the code. In the console, the result is:
12
13
Notice that the ‘never used’ warning is now gone.
If you want to print the increment, move the increment operator, ++, before the variable:
fun main() {
var count = 12
println(++count) // Prints 13
}
Decrement
The decrement operator reduces a numerical value by 1 and updates the operand. It works on variables or objects only. It doesn’t work with every kind of numerical data, and the variable must be mutable. Like the increment operator, the decrement operator may proceed with count-- or precede --count, the data’s variable. The following example shows the pre-decrement and post-decrement operators:
fun main() {
var count = 12
println(count--) // Prints 12
println(count) // Prints 11
println(--count) // Prints 10
}
Run the code. In the console, the result is:
12
11
10