Kotlin uses two keywords to declare variables:
val — read-only (like a final variable)var — mutable (can be reassigned)val name: String = "Alice" // immutable
var age: Int = 25 // mutableKotlin can automatically detect the type, so you can omit the explicit type:
val language = "Kotlin"
var version = 1.9The type will still be statically checked by the compiler.
You can reassign var variables, but not val ones:
var counter = 5
counter += 1 // ✅ OK
val year = 2024
year = 2025 // ❌ Error: val cannot be reassignedval by default for safetyvar only when mutation is neededTo declare compile-time constants, use const val inside top-level or object declarations:
const val PI = 3.14159Ask the AI if you need help understanding or want to dive deeper in any topic