Assigning a new value
A variable declared with var can hold a different value later. You write the name, an equals sign and the new value, with no var in front:
var myVar = 5
myVar = 10
println(myVar)
10
The first line declares myVar and gives it 5. The second line assigns 10 to the existing variable. From then on, every use of myVar sees 10. The old value is gone.
The right side can be any expression, including another variable:
var myVar = 5
var myNum = 10
myVar = myNum
println(myVar)
10
The assignment copies the current value of myNum into myVar. The two variables stay independent afterwards. Changing myNum later doesn't change myVar.
Declaring versus assigning
The lesson's hint warns against writing var again. In Scala that is a different action, not a way of changing the value. Declaring a second var age in the same block is a compile error:
-- [E161] Naming Error ----------------------------------------------------
| var age = 34
| ^^^^^^^^^^^^
| age is already defined as variable age
Remember the split: var creates a name, = alone changes what an existing name holds.
Types stay fixed
Reassignment changes the value, not the type. If age was created as an Int, only an Int can be assigned later. age = "thirty" is rejected with Found: String and Required: Int.
The exercise step by step
The starting program declares role, age and language, then prints a description of Mircea today. After a marked area for your code, it prints the same facts "ten years later". Without your code, both descriptions would be identical.
- Work only in the marked area. The declarations above and the prints below stay as they are.
- Reassign
role. Writerole = "Senior Developer". The text has quotes, andvaris absent. - Reassign
age. Today's age is 24 and ten years later it is 34, so writeage = 34. - Leave
languagealone. He never changed it, so the second description should still say Java.
The solution in Scala
var role = "Junior Developer"
var age = 24
var language = "Java"
// Today
println(s"His age is $age")
println(s"He is a $role")
println(s"He codes in $language")
// ---------------------------------------
// Write your code here:
role = "Senior Developer"
age = 34
// ---------------------------------------
// Ten years later:
println(s"Ten years later, his age is $age")
println(s"He is a $role")
println(s"He still codes in $language")
The program runs from top to bottom. The first three println calls see the original values, because the assignments haven't happened yet. The two assignments then replace the values, and the last three calls see the new ones. The output is:
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 s in front of the strings lets $age, $role and $language insert the variable's value at that moment.
Common mistakes
- Repeating
var. It creates a second variable instead of changing the first, and the compiler rejects it. - Assigning too early or too late. The order of lines matters. An assignment placed above the first prints would change what "today" shows.
- Changing
language. It must stay"Java"so the last line is accurate. - Reassigning a
val. Onlyvarvariables can be reassigned.valis covered in the next lessons. - Computing from the old value. You can also write
age = age + 10orage += 10, which reads the current value, adds 10 and stores the result.
Key takeaways
name = valuechanges avar; the old value is lost.- Don't write
varagain when you only want to change the value. - The new value must have the variable's type.
- Code runs top to bottom, so each line sees the values set before it.
