Instruction 2
Assignment Operators
Assignment operators set the value of the left-hand side operand to evaluate the right-hand side operand. You’ve been using this operand since the beginning of the course. :] Value assignment is fundamental in software programming.
To assign a value to a variable, you put the receiving variable to the left of the equals sign, val count =. Next, set the right side of the equals sign to the calculation you want to assign to the left side, 12:
val count = 12
The way you read the above code is that 12 is assigned to count.
Augmented Assignment Operators
These operators combine an arithmetic operator with the assignment operator. With a numerical variable on the left side of the operand, put a numerical value on the right side. Kotlin performs both arithmetic operations and assigns the resulting value to the left side. count += 3 becomes count = count + 3. Augmented assignment operator examples are +=, -=, *=, /= and %=. Update your code with the following:
fun main() {
var count = 12
count += 3
println(count)
}
Run the code. The result from the console is:
15
This is the same as:
fun main() {
var count = 12
count = count + 3
println(count)
}
Run the code. The result from the console is:
15
If you substitute “+=” for “=” and “+”, the operation behaves like a normal assignment operator.
Exploring Logical Operators
Logical operators are used to perform logical operations. These are operations that always result in a Boolean value, either a true or false.
The logical AND is represented by &&. This operation results in a true if both operands are true:
fun main() {
val isWeekend = true
val hasMoney = true
val isTimeToRelax = isWeekend && hasMoney
println(isTimeToRelax)
}
Run this code, and it will return true. If any of them were false, the result would be false, too.
Kotlin’s implementation of the logical OR operator is ||. This operator results in a true if any of the operands are true. Otherwise, it’s false.
fun main() {
val isWeekend = true
val isHoliday = false
val hasMoney = true
val isTimeToRelax = isHoliday || hasMoney
println(isTimeToRelax) // Returns true since at least one of the operands is true
}
With this operation, you only need one of the operands, in this case, hasMoney, to be true, and the result will be true.
Note: Notice that the IDE again displays the information graphic. The IDE points out that the value
isWeekendis never used. This could be intended or you may want to remove it, making your code easier to read. Or you may have wanted to useisWeekendand forgot. Either way, the information is helpful for you as a developer. I get by with a little help from my friends… :]
Kotlin uses ! to represent the logical NOT operator. This operator negates a true or false value.
fun main() {
val isWeekend = true
val isWeekday = !isWeekend
println(isWeekday) // Returns false since it negated isWeekend which is true
}
Run this code, and it returns false since NOT true is false.
&&, ||, ! - logical ‘and’, ‘or’, ‘not’ operators, for bitwise operations, use the corresponding infix functions instead.
Equality Operators
To compare two values, Kotlin has the equality operator ==. This operator’s equivalent Kotlin function is equals(). This method is available for all objects in Kotlin. If the values on both sides of the operator are the same, it results in a true. Otherwise, it’s false. In the code below, the price of a banana is the same value as the price of a coconut and a pawpaw combined:
fun main() {
val banana = 12
val coconut = 7
val pawpaw = 5
println(banana == coconut + pawpaw) // Returns true since the values are the same on both sides of the operator
}
Run this code, and it returns true since 5 + 7 is equal to 12.
To negate the equality operation, prepend the NOT operator !=
fun main() {
val banana = 12
val coconut = 7
val pawpaw = 8
println(banana != coconut + pawpaw) // Returns true since the values are the not same on both sides of the operator
}
Run this code, and it returns true since 12 is not equal to 7 + 8.
Understanding Referential Equality Operators
Computers store data at specific memory addresses. Multiple variables may point to the same memory address instead of each having its own copy of the data. Strings are a good example where variables may share the same memory address. In this case, different variables contain the same value. Another example is when you assign a variable to another variable. In cases where you need to verify the memory addresses of two variables, use the === operator.
fun main() {
val pet = "Chameleon"
val reptile = "Chameleon"
val amphibian = "Axolotl"
val newPet = amphibian
println(pet === reptile)
println(newPet === amphibian)
}
Run the code. The === asks, is pet the exact value and location of reptile? Since pet is the exact same string as reptile, then the result is true'. You get the same result with newPet === amphibian`.
true
true
The fact that the values of two variables are the same, ==, doesn’t mean their memory addresses are the same as with ===. The value and the address of the data may differ.
Note: To negate the comparison check, use
!==.
Comparing Variables with Comparison Operators
Kotlin’s comparison operators compare two values. > is the greater than operator. < is the lesser than operator. >= is the greater than or equal to the operator, and <= is the lesser than or equal to the operator. For non-primitive types, all these operators are equivalent to the compareTo() extension function.
fun main() {
val banana = 12
val coconut = 7
val pawpaw = 8
println(coconut < pawpaw)
println(banana >= coconut)
println(pawpaw.compareTo(banana))
}
Run the code and you get:
true
true
-1
The compareTo() function returns one of three possible results: -1, 0, or 1. -1 if the operand on the left is greater than the operand on the right. 0 if both operands are the same. 1 if the operand on the right is lesser than the operand on the left.
To negate a comparison, use the opposing operator. > for < and vice versa. You can’t use the NOT operator, !, with these operators.