Open the Starter project in the 03-using-operators directory of the m3-kpl-materials repo in Android Studio Hedgehog or later.
Wait for the project to finish building.
Create a new scratch file in the com.yourcompany.android.meetkotlin package.
In this lesson, you’ll practice working with the different operators Kotlin provides.
In your scratch file, type the following snippet:
val sum = 3+2
println(sum)
In the snippet above:
-
You create a variable called sum and assign it an expression
3+2. - You then printed out the value of the sum, which is 5.
In this snippet, + is the arithmetic operator which computes the sum of the two operands.
Similarly, you can compute the difference, quotient product and remainder. To do so, enter the following snippet in the scratch file:
val difference = 10 - 2
println(difference)
val product = 3 * 2
println(product)
val quotient = 6 / 2
println(quotient)
val remainder = 11 % 2
println(remainder)
In the snippet above, you used:
- - to compute the difference.
- * to compute the product.
- / to compute the quotient
- % to compute the remainder
These are the arithmetic operators you can use in Kotlin.
At this point in the course, you’ve already used the primary assignment operator several times. It’s worth noting that Kotlin also offers compound assignment operators that let you combine an arithmetic operation with the assignment operation.
To understand how compound assignment operators work, type the following snippet in your scratch file:
// 1
var count = 5
// 2
println(count)
// 3
count += 1
println(count)
// 4
count -= 3
println(count)
// 5
count *= 2
println(count)
// 6
count /= 2
println(count)
In the snippet above you:
- Created a mutable variable count and assigned it an integer value of 5.
- Then, printed the initial value.
- Then, used the += compound operator to add 1 to the initial value and printed it.
- Next, used the -= compound operator to subtract 1 from count and printed it.
- Then, used the *= compound operator to multiply the count by 2 and printed it.
- Finally, used the /= compound operator to divide the count by 2 and printed it.
Kotlin offers handy, unary operators to increment and decrement integer values.
Enter the following snippet at the end of your scratch file:
// 1
count ++
// 2
println(count)
// 3
count--
// 4
println(count)
In this snippet you:
- Incremented the value of count by 1 using the ++ increment operator.
- Then, printed the new value of count.
- Next, decremented count’s value by 1 using the – decrement operator.
- Finally, printed the new value of count.
Relational operators let you compare two values, and they return a Boolean result. They’re helpful when you want to branch your code and execute certain blocks based on the result of a value check.
In your scratch file, enter the following snippet:
// 1
val age = 14
// 2
if (age < 16) {
// 3
println("You can't drive")
// 4
} else {
println("You can drive")
}
In the next lesson, you’ll learn more about if-else statements, but they help illustrate relational operators here:
- You first created a variable called age and assigned it a value of 14.
- You then created a relational check using the < operator to check if the age is less than 16.
- Next, you created a new block that’ll print “You can’t drive” if the age is less than 16.
- Finally, you created an else block, which will printed “You can drive” if the age is greater than 16.
Logical operators let you perform logical checks and return a Boolean value. They’re often used in conjunction with relational operators.
Enter the following snippet in your scratch file:
// 1
val a = 10
val b = 20
val c = 30
// 2
val isALargest = a > b && a > c
// 3
println("It is ${isALargest} that a is the largest number")
Here’s a breakdown of what’s going on in the snippet above:
- You first created three variables - a, b, and c, and assigned them integer values of 10, 20, and 30, respectively.
- You then created another variable, isALargest, to hold the result of the logical expression that checks if a is greater than b AND if a is also greater than c.
- You then printed a message to the console.
Next, enter the following snippet in your scratch file:
// 1
val raining = true
// 2
val temperature = 35
// 3
val carryUmbrella = raining || temperature > 30
// 4
println("Carry an umbrella: $carryUmbrella")
In the snippet above you:
- Created a variable raining and assigned it to true.
- Then, created a variable temperature and assigned it to 35.
- Next, created a variable carryUmbrella and assigned it the logical expression that checks if raining is true OR temperature is more than 30.
- Finally, printed a message to the console with the value of carryUmbrella.
Finally, enter the following snippet to learn about the logical NOT operator:
// 1
val isTrue = true
// 2
println(!isTrue)
- Here, you created a new variable called isTrue that points to true.
- You then printed out the result of performing a logical NOT on the value of isTrue, which results in inverting the variable’s value.
In addition to the operators you saw earlier, Kotlin offers two special operators for working with null values.
Enter the following snippet in your scratch file:
val hello: String? = null
println(hello?.length)
val message = hello ?: "value is null"
println(message)
The first operator here is a safe access operator. You use ?. to access the length property on the string hello safely.
The second operator, ?:, is known as the Elvis operator. The line using it essentially says, if hello holds a non-null value give me that, else give me the string "value is null".