Data Types
Primitive Types
Use numbers and booleans in simple expressions.
types
Kotlin infers common number and boolean types from literal values.
Primitive Types
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")
}
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
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
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
quantitystarts at4.unitPriceis6.subtotal = quantity * unitPricebecomes24.qualifies = subtotal >= 24becomestrue.- The program prints
subtotal=24andqualifies=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.