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.
- Reassign
role. Writerole = "Senior Developer";with no type in front. The variable was declared at the top as aString, so only the name and the new value are needed. - Reassign
age. Ten years after 24 is 34, so writeage = 34;. - Leave
languagealone. 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 witherror: 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";givesincompatible types: String cannot be converted to int, andage = 34.5;givesincompatible 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
varkeeps its type too.var v = 5;makesvanint, and a laterv = 7;is fine, butv = "seven";would not compile. finalblocks reassignment. Afterfinal int limit = 10;, the linelimit = 20;fails withcannot assign a value to final variable limit. Usefinalfor 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;givesvariable n might not have been initialized. Declaring first and assigning later, as inint 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 + 1works. - The new value must be compatible with the declared type.
- Reassigning one variable does not change other variables that were copied from it.
