Adding a Number to a Variable

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Until some other statement puts a different value into it, or until the program ends.

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 the following program print out:

// Example assignment statements
int extra, value;

extra = 5;
value = extra + 2;
System.out.println( "value now holds: " + value );