Assigning a new value
A variable declared with var can be given a new value after it is created. You do that with the assignment operator =, and without repeating the keyword:
// Initializing with 5:
var myVar = 5
// Assigning the value 10:
myVar = 10
println(myVar) // 10
The first line declares myVar and gives it the value 5. The second line doesn't create anything. It replaces the stored value with 10. From then on, every use of myVar sees 10, and the 5 is gone.
The right side of = is evaluated first, so it may use the variable's own old value: age = age + 1 reads the current age, adds one and stores the result. Kotlin offers shorthand for this, age += 1 or age++.
Copying one variable into another
The right side can also be another variable:
var myVar = 5
val myNum = 10
myVar = myNum
println(myVar) // 10
Kotlin reads the current value of myNum, which is 10, and stores it in myVar. This copies the value. The two variables are not linked afterwards: if myNum were a var and you changed it later, myVar would keep the 10 it already holds.
Rules that come with reassignment
- Only
varcan be reassigned. A name declared withvalis fixed once it has a value, and assigning to it is a compile error:'val' cannot be reassigned. - Don't repeat the keyword. Writing
var role = "Senior Developer"again doesn't updaterole; it tries to declare a second variable with the same name in the same scope, and the compiler reportsconflicting declarations. - The type stays the same. A variable that started as an
Intcan only receiveIntvalues later.var x = 5followed byx = 5.5is rejected, and so is assigning text to it. - Order matters. Code runs from top to bottom. A
printlnplaced above the reassignment prints the old value, and one placed below prints the new one.
Two worked examples
Because the right side is evaluated before the store happens, a variable can be updated in terms of itself, and values can be moved between variables in order:
var score = 10
score = score + 5 // 15
score += 5 // 20
score++ // 21
println(score)
Each line reads the current score and stores a new one. The output is 21.
Copying is a one-time event, as this shows:
var x = 5
var y = x
x = 99
println("$x $y")
It prints 99 5. The line var y = x copied 5 into y; the later change to x doesn't reach it.
Swapping two variables needs a third one to hold a value temporarily. Without it, the first assignment would overwrite a value you still need:
var a = 1
var b = 2
val temp = a
a = b
b = temp
println("$a $b")
This prints 2 1.
The exercise
The starter code describes a developer with three variables and prints them twice. The first block of prints shows his situation today. The second block, at the bottom, says "Ten years later". The space between them is where you write code:
var role = "Junior Developer"
var age = 24
var language = "Java"
// Today
println("His age is $age")
println("He is a $role")
println("He codes in $language")
// ---------------------------------------
// Write your code here:
// ---------------------------------------
// Ten years later:
println("Ten years later, his age is $age")
println("He is a $role")
println("He still codes in $language")
Ten years later he is a senior developer, ten years older, and he still codes in Java. So two variables change and one doesn't:
- Reassign
roleto"Senior Developer". - Reassign
ageto34, which is 24 plus ten. - Leave
languagealone. The text says he still codes in Java, and the variable already holds that.
var role = "Junior Developer"
var age = 24
var language = "Java"
// Today
println("His age is $age")
println("He is a $role")
println("He codes in $language")
// ---------------------------------------
// Write your code here:
role = "Senior Developer"
age = 34
// ---------------------------------------
// Ten years later:
println("Ten years later, his age is $age")
println("He is a $role")
println("He still codes in $language")
The output shows the change:
His age is 24
He is a Junior Developer
He codes in Java
Ten years later, his age is 34
He is a Senior Developer
He still codes in Java
The first three lines print before the reassignment, so they show the old values. The last three print after it. The println lines are identical in the two blocks, and only the values behind the variables have moved. Writing age = age + 10 would work equally well here and shows where 34 comes from.
Key takeaways
name = newValuereplaces the value of an existingvar.- Don't write
varagain when reassigning. - A
valcan't be reassigned; the compiler refuses. - The new value must have the variable's type.
- Statements run in order, so each line sees the values set by the lines above it.