Instruction 2

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.

  val count = 12

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)
}
15
fun main() {
  var count = 12
  count = count + 3
  println(count)
}
15

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.

fun main() {
  val isWeekend = true
  val hasMoney = true
  val isTimeToRelax = isWeekend && hasMoney
  println(isTimeToRelax)
}
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
}
Fig. 3.1 - 'Variable in never used' warning
Bax. 7.5 - 'Gefeohmu oj jakok ewip' vihvivw

fun main() {
  val isWeekend = true
  val isWeekday = !isWeekend
  println(isWeekday) // Returns false since it negated isWeekend which is true
}

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
}
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
}

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)
}
true
true

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))
}
true
true
-1
See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Instruction 3