The program will print out:
value now holds: 7
Look at the statements:
value = 5; value = 12 + value;
Assume that value
has already been declared.
The two statements execute one after the other,
and each statement performs two steps.
The first statement:
value
.
The second statement:
12 + value
.
value
to get the number 5.value
.
Note: A variable can be used on both the LEFT and the RIGHT of the =
in the same assignment statement.
When it is used on the right, it provides a number used to calculate a value.
When it is used on the left, it says where in memory to save that value.
The two roles are in separate steps, so they don't interfere with each other. Step 1 uses the original value in the variable. Then step 2 puts the new value (from the calculation) into the variable.
What does the following program fragment write?
value = 5; System.out.println("value is: " + value ); value = value + 10; System.out.println("value is: " + value );