Start learning free

LESSON 12 OF 348 · VARIABLES

Variable Reassignment in Kotlin: Changing a var's Value

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
val 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

    KOTLIN · VARIABLES

    Variable Reassignment in Kotlin, explained

    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 var can be reassigned. A name declared with val is 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 update role; it tries to declare a second variable with the same name in the same scope, and the compiler reports conflicting declarations.
    • The type stays the same. A variable that started as an Int can only receive Int values later. var x = 5 followed by x = 5.5 is rejected, and so is assigning text to it.
    • Order matters. Code runs from top to bottom. A println placed 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:

    1. Reassign role to "Senior Developer".
    2. Reassign age to 34, which is 24 plus ten.
    3. Leave language alone. 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 = newValue replaces the value of an existing var.
    • Don't write var again when reassigning.
    • A val can'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.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free