Kotlin Variables
A variable is a named box that holds a value. Kotlin has two keywords for creating one, and choosing between them is one of the first habits you'll build.
| Keyword | Meaning | Can be reassigned? |
|---|---|---|
val |
value — read-only | no |
var |
variable — mutable | yes |
fun main() {
val city = "Lisbon" // read-only
var temperature = 18 // mutable
println("$city: $temperature °C")
temperature = 22
println("$city: $temperature °C")
}Lisbon: 18 °C
Lisbon: 22 °C
val cannot be reassigned
Try to change a val and the compiler refuses — the program never runs:
fun main() {
val city = "Lisbon"
city = "Porto"
println(city)
}File.kt:3:5: error: 'val' cannot be reassigned.
Tip: Prefer
valeverywhere and switch tovaronly when you genuinely need to change the value. Read-only variables make code easier to reason about — when you see aval, you know its value for the rest of the scope.
Type inference and explicit types
You rarely need to write a type: Kotlin infers it from the initial value. You can still spell it out after a colon, which is useful for documentation or when the inferred type isn't the one you want.
fun main() {
val name = "Ana" // inferred: String
val age: Int = 29 // explicit
val height: Double = 1.68
val likesTea: Boolean = true
println("$name, $age, $height m, likes tea: $likesTea")
}Ana, 29, 1.68 m, likes tea: true
Once a variable has a type, it keeps it. Assigning a value of another type is a compile error:
fun main() {
var count = 10
count = "ten"
}File.kt:3:11: error: Assignment type mismatch: actual type is 'String', but 'Int' was expected.
Declaring now, assigning later
A variable may be declared without a value as long as you give its type and assign it before reading it. The compiler tracks this for you:
fun main() {
val greeting: String
val hour = 9
if (hour < 12) {
greeting = "Good morning"
} else {
greeting = "Good afternoon"
}
println(greeting)
}Good morning
Even though greeting is a val, this is fine: it is assigned exactly once on every path.
Constants
For values known at compile time (numbers, strings, booleans) declared at the top level or in an object, use const val. By convention their names are in upper case:
const val MAX_USERS = 100
const val APP_NAME = "Notes"Naming rules
- Names may contain letters, digits and underscores, but can't start with a digit.
- Names are case-sensitive:
totalandTotalare different variables. - Keywords such as
val,funorifcan't be used as names. - Use descriptive camelCase names:
orderTotal, nototorx1.
Note:
valmeans the variable can't be reassigned, not that the object it points to is frozen. Aval list = mutableListOf(1)can still have items added — you just can't pointlistat a different list.
Test yourself
Which keyword declares a variable that can be reassigned?
- val
- var
- const
- let
What is the type of x in val x = 3.0?
- Int
- Float
- Double
- Number
What happens when you assign a String to a variable inferred as Int?
- It is converted automatically
- A compile error
- The program crashes at runtime
- The variable becomes Any