Part 02 / Topic 5 of 8
Operators: arithmetic, comparison and logical
// Arithmetic operators: + - * / %
val subtotal = 2400
val diners = 4
println("Subtotal: $subtotal, diners: $diners")
println("Share per head, integer division: ${subtotal / diners}")
// Integer division truncates; it never rounds, and it never produces a fraction
val tricky = 7 / 2
println("7 / 2 as Int division: $tricky")
val notTricky = 7.0 / 2
println("7.0 / 2 as Double division: $notTricky")
// % is the remainder, not a percentage
println("7 % 2 (remainder): ${7 % 2}")
// Compound assignment: shorthand for "take the current value, combine, store back"
var tab = 0.0
tab += 320.0 // same as tab = tab + 320.0
tab += 180.0
tab -= 20.0 // a discount
println("Tab after compound assignment: $tab")
// ++ and -- also mutate in place, prefix and postfix differ in what they hand back
var guestCount = 3
val before = guestCount++ // reads 3, then increments
val after = ++guestCount // increments first, then reads 5
println("before=$before, after=$after, guestCount=$guestCount")
// Comparison operators: == compares value (calls equals()), not reference identity
val nameA = "Arjun"
val nameB = "Arjun"
println("nameA == nameB: ${nameA == nameB}") // true: same value
println("nameA === nameB: ${nameA === nameB}") // true here too, but === checks identity, not value
// Logical operators: && and || short-circuit: the right side only runs if it can change the answer
val hasReservation = true
val isVip = false
println("hasReservation && isVip: ${hasReservation && isVip}")
println("hasReservation || isVip: ${hasReservation || isVip}")
println("!isVip: ${!isVip}")
$ kotlinc 04_operators.kt -include-runtime -d 04.jar
$ java -jar 04.jar
Subtotal: 2400, diners: 4
Share per head, integer division: 600
7 / 2 as Int division: 3
7.0 / 2 as Double division: 3.5
7 % 2 (remainder): 1
Tab after compound assignment: 480.0
before=3, after=5, guestCount=5
nameA == nameB: true
nameA === nameB: true
hasReservation && isVip: false
hasReservation || isVip: true
!isVip: true
Four things worth remembering from this one:
/between twoInts stays anInt.7 / 2is3, silently, with no warning — make at least one side aDouble(7.0 / 2, or.toDouble()on a variable) whenever you actually want a fraction back.%is the remainder of division, not "percent."7 % 2is1— the part left over after taking out as many whole2s as fit into7. Percentages (GST, tip) are built with*and/in section 8,%plays no part in them.==is Kotlin's "do these hold the same value" check, equivalent to Java's.equals(), not Java's reference-comparing==.===is the one that checks "is this the exact same object in memory" — reach for==almost always;===only when identity itself is the question.&&/||short-circuit: the right-hand side is skipped entirely once the left side already decides the answer (false && anythingisfalsewithout looking at "anything"). This matters once a condition's right side has a side effect or could fail — Part 4 shows that case for real, once there's a function call worth short-circuiting around.
Full program: examples/04_operators.kt.
Done here? The practice set for Part 2 — including the solutions — is on the part page.