TOPIC 1.4 · STUDY

Assignment Statements and Input

An assignment stores a value in a variable. A later assignment replaces that value. Copying one int into another stores the current number only. Input is a value the user types; it must be read into a variable before the program can use it. Output is text the program sends with System.out.print or System.out.println.

1.4.A

Assignment statements

An assignment stores a value in a variable. The left side names the variable. The right side is evaluated first, then that result is stored. After the assignment finishes, the variable holds the new value.

int crates = 12;
crates = crates + 5;
crates = crates - 2;
System.out.print(crates);

crates starts at 12. The next line replaces it with 17. The line after that replaces it with 15. The earlier values are gone. A later assignment does not keep a history; it overwrites what was there.

After this statement Value in crates
int crates = 12; 12
crates = crates + 5; 17
crates = crates - 2; 15

Assigning one int variable to another copies the current value. The two variables are separate after that. Changing one later does not change the other.

int stock = 8;
int backup = stock;
stock = stock - 3;
System.out.print(backup);
Three stages. First stock holds 8. Then backup copies that 8. Then stock becomes 5 while backup still holds 8.

backup received the value 8 at the moment of the copy. Later changes to stock leave backup alone.

That printed result is 8. stock ends at 5, but backup never changed. The right side of backup = stock used the value stock held at that moment, then stored a copy.

Key points

  • An assignment stores a value in a variable.
  • A later assignment replaces the previous value.
  • Assigning one int to another copies the current value only.
  • After a copy, changing one variable does not change the other.

1.4.B

Input and output

Input is a value the user types that enters the program. Output is information the program sends out, usually with System.out.print or System.out.println. Typing on the keyboard is not enough by itself. The program must read the typed value into a variable before it can use that value in calculations or later statements.

This course uses helper methods for input. Input.readInt() reads the next integer the user types and returns it as int. Each call consumes one integer. Input.readDouble() reads the next decimal number and returns it as double. Input.readWord() reads the next whitespace-delimited word and returns it as String. Each of those calls also consumes one value of its kind.

A typed integer moves left to right into a variable through Input.readInt. A separate arrow leaves the variable downward through System.out.print to the screen as output.

The top row is input: a typed integer is read and stored. The downward arrow is output: the program prints a value it already holds.

System.out.print("How many boxes?");
int boxes = Input.readInt();
int total = boxes * 4;
System.out.print(total);

The first print is output: a prompt on the screen. It does not store what the user types. Input.readInt() is the input step. If the user types 7, boxes stores 7 and total becomes 28. The last print is output again.

Action Direction Example
User types a number Input 7 entered at the keyboard
Read into a variable Input int boxes = Input.readInt();
Show a prompt or result Output System.out.print(total);

Two typed values need two helper calls. Each call consumes one value. The same program can print different results on different runs when the user types different numbers.

Key points

  • Input enters the program; output leaves it.
  • System.out.print and System.out.println produce output.
  • Input.readInt(), Input.readDouble(), and Input.readWord() read typed values.
  • Each helper call consumes one value of its kind.
  • A typed value is usable only after a read stores it in a variable.

A number the user types is not in a variable until a read statement stores it. A prompt alone does not capture input. Write a statement such as int boxes = Input.readInt(); before you use that typed value.

Practice this topic