Start learning free

LESSON 12 OF 414 · VARIABLES

Variable Reassignment in Scala: Changing a var with =

Lesson Read the full explanation

You can always change the value stored in a variable with the assignment operator(=):

// Initializing with 5:
var myVar = 5

// Assigning the value 10:
myVar = 10

println(myVar) // Output: 10

First, this code creates a variable named myVar and initializes it with 5.

Then, the code assigns 10 to myVar. Now, when myVar appears again in the code, the program will treat it as if it is 10.


Assigning the Value of One Variable to Another:

You can also assign the value of some variable to another variable using the assignment operator:

var myVar = 5
var myNum = 10

myVar = myNum

println(myVar) // Output: 10

First, this code creates a variable named myVar and initializes it with 5 and another variable named myNum and initializes it with 10.

Then, the contents of myNum (which is 10) is assigned to the variable myVar. Now, myVar also has the value of 10.


Assignment
Follow the Coding Tutorial and let's reassign some variables.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · VARIABLES

    Variable Reassignment in Scala, explained

    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.

    1. Work only in the marked area. The declarations above and the prints below stay as they are.
    2. Reassign role. Write role = "Senior Developer". The text has quotes, and var is absent.
    3. Reassign age. Today's age is 24 and ten years later it is 34, so write age = 34.
    4. Leave language alone. 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. Only var variables can be reassigned. val is covered in the next lessons.
    • Computing from the old value. You can also write age = age + 10 or age += 10, which reads the current value, adds 10 and stores the result.

    Key takeaways

    • name = value changes a var; the old value is lost.
    • Don't write var again 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.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free