Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

14. Adding a Number to a Variable


Answer:

    quantity holds: 13
The assignment statement replaced the value that was originally in quantity with the value 13.

Adding a Number to a Variable

Assume that extra already contains the value 5.

Extra with 5

Here is another statement:

value = extra + 2;

The statement will be performed in two steps (as always). The first step performs the calculation extra + 2 by first copying the 5 from extra, and then adding 2 to it:

Do First

The result of the calculation is 7. The second step puts 7 into the variable value:

Do Second

Question 15:

What will this program print out:

    // Example assignment statements
    int extra, value; extra = 5; value = extra + 2;
    System.out.println( "value now holds: " + value );