Start learning free

LESSON 9 OF 414 · VARIABLES

The Power of Variables in Scala: Change a Value Once

Lesson Read the full explanation

Now that we've learned about variables, let's see why they are so important in our programmers' life:

Consider this simple program which prints a story:

println("My name is John and I am 40 years old")
println("Coding at age 40 is really fun")
println("My son is named after me, John")
println("I'd love him to be as cool as me at age 40")
println("It's so funny when my wife calls John we both respond")

Changing the story:

This is a perfectly working program. But what if I come back to it and want to change the character's name to Andy and his age to 32?

Well, I'm going to have to go through the entire program and manually change the name John to the name Andy and the age 40 to the age 32:

println("My name is Andy and I am 32 years old")
println("Coding at age 32 is really fun")
println("My son is named after me, Andy")
println("I'd love him to be as cool as me at age 32")
println("It's so funny when my wife calls Andy, we both respond")

This is not horrible as I had to go through only 5 lines of code. But what if my code consisted of hundreds of lines?

It's a disaster. Going through the whole code and manually changing the name and age is boring but more importantly it's super easy to make mistakes.


The story with variables:

This is where variables shine. If I used a variable to store the name of the character and another variable to store his age when I first wrote this code, it would have looked like this:

var name = "John"
var age = 40

println("My name is " + name + " and I am " + age + " years old")
println("Coding at age " + age + " is really fun")
println("My son is named after me, " + name)
println("I'd love him to be as cool as me at age " + age)
println("It's so funny when my wife calls " + name + " we both respond")

Changing the story with variables:

Now whenever I want to change the character's name and age, I only have to change two lines of code, instead of possibly hundreds.

If I want my character to be named Andy and aged 32, I initialize the variables name with Andy and age with 32 and leave the rest of the code untouched:

var name = "Andy"
var age = 32

println("My name is " + name + " and I am " + age + " years old")
println("Coding at age " + age + " is really fun")
println("My son is named after me, " + name)
println("I'd love him to be as cool as me at age " + age)
println("It's so funny when my wife calls " + name + " we both respond")

Assignment
Follow the Coding Tutorial and let's witness the power of variables!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · VARIABLES

    The Power Of Variables in Scala, explained

    The problem with repeated values

    Imagine a program that tells a short story about a character called John, aged 40. The name and the age appear several times:

    println("My name is John and I am 40 years old")
    println("Coding at age 40 is really fun")
    println("My son is named after me, John")
    println("I'd love him to be as cool as me at age 40")
    

    It works, but changing the character to Andy, aged 32, means finding and editing every occurrence by hand. With four lines that is easy. With hundreds of lines, it is slow, and it is easy to miss one and leave a story that says Andy in one place and John in another.

    Store the value once

    A variable gives a value a name. Define it once, then use the name wherever the value belongs:

    var name = "John"
    var age = 40
    
    println("My name is " + name + " and I am " + age + " years old")
    println("Coding at age " + age + " is really fun")
    println("My son is named after me, " + name)
    

    To tell the story about Andy, you edit the two definitions and nothing else. Every line that uses name or age follows along. That is the point of variables: a fact lives in one place, and everything else refers to it.

    String interpolation

    The exercise uses a tidier way to put variables into text. A string with an s in front of its opening quote is an interpolated string, and $ followed by a name inserts that variable's value:

    var experience = 5
    var language = "Python"
    println(s"I have $experience years of experience with $language")
    
    I have 5 years of experience with Python
    

    Numbers need no conversion: the value of experience is written into the text as 5. Without the leading s, the program would print the characters $experience as they stand. To insert a calculation rather than a bare name, use braces, as in s"next year: ${experience + 1}".

    The exercise step by step

    The starting code already contains the story as one println line and two variables. The task is to turn it into a story about 13 years of experience with Java without touching the println line.

    1. Leave the print line alone. It refers to the variables by name, so it needs no change.
    2. Edit experience. Replace 5 with 13. This is a number, so it has no quotes.
    3. Edit language. Replace "Python" with "Java". This is text, so the quotes stay.
    4. Keep the declarations intact. Only the part right of = changes. The words var and the variable names stay as they are.

    The solution in Scala

    // Create variables here:
    var experience = 13
    var language = "Java"
    
    // Print story here:
    println(s"I have $experience years of experience with $language")
    

    It prints:

    I have 13 years of experience with Java
    

    Notes on the declarations:

    • Types are inferred. Scala decides that experience is an Int and language is a String from the values, so no type annotation is needed.
    • var allows later changes. Both variables could be reassigned later in the program. A value that never changes is better declared with val, which later lessons explain.
    • Comments start with // and run to the end of the line. Scala ignores them.

    Common mistakes

    • Editing the story instead of the variable. If you rewrite the println line, you lose the benefit. Change the data, not the template.
    • Forgetting the s prefix. The output then contains $experience and $language literally.
    • Quoting a number. var experience = "13" makes a String. The story would still print the same, but the variable could no longer be used in arithmetic.
    • Leaving out the quotes on text. var language = Java asks Scala for a name Java, which isn't defined, and the program doesn't compile.

    Key takeaways

    • A variable stores a value once, so the rest of the program can refer to it by name.
    • Changing the variable updates every place that uses it.
    • The s"..." form with $name inserts values into text.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free