Thuta Learning
BasicProgrammingbeginner

Variables (val vs var)

Relax. We'll talk through this in plain words — no textbook voice.

Kotlin has two keywords for storing values. val is for a value you set once and never change; var is for a value you can change later. As a project grows, knowing which variables can change and which values shouldn't is a basic habit that cuts down on bugs.

kotlin
fun main() {
    val language = "Kotlin"
    // language = "Java" // Error: val ကို ပြန် assign လုပ်လို့မရပါ။

    var version = 1
    version = 2

    println("Language: $language")
    println("Version: $version")
}
You should see
Language: Kotlin Version: 2

Summary

val = stable value, var = changeable value — keep that in mind.

Easy traps

  • It's common to use var for everything at first. Using val for values that don't need to change is the safer habit.

Practical example — shopping cart count

Practical example — shopping cart count

kotlin
fun main() {
    val productName = "Wireless Mouse"
    var quantity = 1

    quantity += 2

    println("Product: $productName")
    println("Quantity: $quantity")
}

You'll know it worked when: Product: Wireless Mouse Quantity: 3

Variables (val vs var) | Thuta Learning