Start learning free

LESSON 12 OF 410 · VARIABLES

Variable Reassignment in Java: Changing a Variable's Value

Lesson Read the full explanation

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

// Initializing with 5:
int myVar = 5;

// Assigning the value 10:
myVar = 10;

System.out.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:

int myVar = 5;
int myNum = 10;

myVar = myNum;

System.out.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

    JAVA · VARIABLES

    Variable Reassignment in Java, explained

    Changing the value of a variable

    A variable in Java is a named box that holds one value. After you create it, you can put a different value in the box as many times as you like. This is called reassignment, and it uses the same assignment operator = that gave the variable its first value:

    int myVar = 5;   // declaration and first value
    myVar = 10;      // reassignment
    System.out.println(myVar);
    

    This prints 10. The key difference between the two lines is the type. The first line starts with int, which declares a new variable. The second line has no type, so it refers to the variable that already exists. After the second line, the old value 5 is gone, and every later use of myVar sees 10.

    How to change a variable in Java

    To update a variable, write its name, =, and the new value. The new value can be a literal, another variable or any expression, including one that uses the variable itself:

    int myVar = 5;
    int myNum = 10;
    
    myVar = myNum;        // copy the value of myNum into myVar
    myVar = myVar + 1;    // use the old value to compute the new one
    myVar += 10;          // shorthand for myVar = myVar + 10
    myVar++;              // shorthand for adding 1
    System.out.println(myVar);
    

    This prints 22: 10 copied from myNum, plus 1, plus 10, plus 1. The right side of = is calculated first, using the current values, and only then is the result stored in the variable on the left. That is why myVar = myVar + 1 makes sense even though it would be impossible as an equation.

    Copying one variable into another copies the value at that moment. For primitive types such as int, the two variables stay independent afterwards:

    int x = 5;
    int y = x;
    x = x + 1;
    System.out.println(x + " " + y);
    

    This prints 6 5. Changing x did not touch y. Strings behave the same way for a different reason: a String object never changes, so building a new string in a variable only moves that variable to the new object.

    The exercise: ten years later

    The lesson starts with three variables, role (the text "Junior Developer"), age (24) and language ("Java"). The program prints them, and then prints them again under "Ten years later". The task is to make the second report true by reassigning, in the marked area between the two reports.

    1. Reassign role. Write role = "Senior Developer"; with no type in front. The variable was declared at the top as a String, so only the name and the new value are needed.
    2. Reassign age. Ten years after 24 is 34, so write age = 34;.
    3. Leave language alone. He never changed his language, so the second report must still say Java. Not touching a variable keeps its value.
    String role = "Junior Developer";
    int age = 24;
    String language = "Java";
    
    // Today
    System.out.println("His age is " +  age);
    System.out.println("He is a " + role);
    System.out.println("He codes in " + language);
    
    // ---------------------------------------
    // Write your code here:
    
    role = "Senior Developer";
    age = 34;
     
    // ---------------------------------------
    
    // Ten years later:
    System.out.println("Ten years later, his age is " +  age);
    System.out.println("He is a " + role);
    System.out.println("He still codes in " + language);
    

    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 first three lines use the old values, because the variables are printed before they change. Order matters: a reassignment affects only the code that comes after it.

    Rules and errors to know

    • Do not write the type again. Declaring int age = 34; in the same scope fails with error: variable age is already defined in method main(String[]). A name can be declared once per scope; after that, assign without a type.
    • The new value must fit the type. A variable keeps its declared type for life. age = "thirty"; gives incompatible types: String cannot be converted to int, and age = 34.5; gives incompatible types: possible lossy conversion from double to int. To store a double in an int on purpose, cast it: age = (int) 34.5; keeps 34.
    • A variable declared with var keeps its type too. var v = 5; makes v an int, and a later v = 7; is fine, but v = "seven"; would not compile.
    • final blocks reassignment. After final int limit = 10;, the line limit = 20; fails with cannot assign a value to final variable limit. Use final for values that should never change.
    • Read before you assign. A local variable declared without a value cannot be used until it is assigned: printing int n; gives variable n might not have been initialized. Declaring first and assigning later, as in int n; n = 3;, is allowed.
    • Reassigning is not the same as redefining. People who search for how to "redefine" a variable in Java usually want reassignment. Java has no way to give an existing name a new type within the same scope.

    Key takeaways

    • Declare a variable once, with its type. Change it later with name = newValue; and no type.
    • The right side is evaluated first, using the current values, so x = x + 1 works.
    • The new value must be compatible with the declared type.
    • Reassigning one variable does not change other variables that were copied from it.
    Keep going with JavaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free