Use numbers and booleans in simple expressions.

types Kotlin infers common number and boolean types from literal values.

Primitive Types

quantity
PrimitiveTypes.kt
Replay: real traced execution (multi-file project)
fun main() {
    val quantity = 4
    val unitPrice = 6
    val subtotal = quantity * unitPrice
    val qualifies = subtotal >= 24

    println("subtotal=$subtotal")
    println("qualifies=$qualifies")
}
fun main() {
    val quantity = 1
    val unitPrice = 6
    val subtotal = quantity * unitPrice
    val qualifies = subtotal >= 24

    println("subtotal=$subtotal")
    println("qualifies=$qualifies")
}
fun main() {
    val quantity = 8
    val unitPrice = 6
    val subtotal = quantity * unitPrice
    val qualifies = subtotal >= 24

    println("subtotal=$subtotal")
    println("qualifies=$qualifies")
}
  1. quantity ← 4, unitPrice ← 6, subtotal ← 24, qualifies ← true

    1fun main() {2    val quantity→ 4 = 4 //@quantity=1, 83    val unitPrice→ 6 = 64    val subtotal→ 24 = quantity4 * unitPrice65    val qualifies→ true = subtotal24 >= 2467    println("subtotal=$subtotal24")8    println("qualifies=$qualifiestrue")9}
    outputsubtotal=24
    qualifies=true
  1. quantity ← 1, unitPrice ← 6, subtotal ← 6, qualifies ← false

    1fun main() {2    val quantity→ 1 = 13    val unitPrice→ 6 = 64    val subtotal→ 6 = quantity1 * unitPrice65    val qualifies→ false = subtotal6 >= 2467    println("subtotal=$subtotal6")8    println("qualifies=$qualifiesfalse")9}
    outputsubtotal=6
    qualifies=false
  1. quantity ← 8, unitPrice ← 6, subtotal ← 48, qualifies ← true

    1fun main() {2    val quantity→ 8 = 83    val unitPrice→ 6 = 64    val subtotal→ 48 = quantity8 * unitPrice65    val qualifies→ true = subtotal48 >= 2467    println("subtotal=$subtotal48")8    println("qualifies=$qualifiestrue")9}
    outputsubtotal=48
    qualifies=true

Follow the Numbers

  1. quantity starts at 4.
  2. unitPrice is 6.
  3. subtotal = quantity * unitPrice becomes 24.
  4. qualifies = subtotal >= 24 becomes true.
  5. The program prints subtotal=24 and qualifies=true. | quantity | unitPrice | subtotal | qualifies | | --- | --- | --- | --- | | 1 | 6 | 6 | false | | 4 | 6 | 24 | true | | 8 | 6 | 48 | true |

Exercise: PrimitiveTypes.kt

Reproduce qualifies=true for subtotal=24, then use the pinned quantities 1 and 8 to predict subtotal and qualifies.