AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-4)
Unit 1 · Topic 1.4
1.4 Assignment Statements and Input
Assignment is how a program stores and updates information. Once you read = as "store the value on the right into the variable on the left", tracing code becomes a matter of keeping careful track of each variable's current value.
Key terms
- assignment operator
- initialize
- expression
nullScanner
How assignment works
The assignment operator = does two things in order. First, Java works out the value of the expression on the right. Then it stores that value in the variable on the left, replacing whatever was there.
That's why count = count + 1; makes sense in Java even though it would be false in algebra. Java reads the current value of count, adds 1, and stores the result back in count. The = sign means "store", not "is equal to". (Checking whether two values are equal uses ==, which you'll see in 2.2.)
An expression is any piece of code that works out to a single value, such as y * 2 + 1 or "Hi " + name. Every expression's value has a type: y * 2 is an int if y is an int.
Initializing variables
A variable has to be given a value before you use it. Its first assignment is called initializing it. Using a local variable that hasn't been initialized is a compile error:
int total;
total = total + 5;
The compiler rejects the second line, because total has no value yet to add 5 to.
The value you store must have a compatible type. You can store an int in a double variable (double d = 5; stores 5.0), but not a double in an int variable without a cast.
A reference variable can hold a reference to an object, or it can hold null, a special value meaning it doesn't refer to any object right now: String nickname = null;.
Tracing with a table
To trace assignment statements, keep a table with one column per variable and update it line by line. Here's the classic swap, which exchanges the values of two variables using a temporary third one:
int a = 4;
int b = 9;
int temp = a;
a = b;
b = temp;
System.out.println(a + " " + b);
| After line | a | b | temp |
|---|---|---|---|
| 1 | 4 | – | – |
| 2 | 4 | 9 | – |
| 3 | 4 | 9 | 4 |
| 4 | 9 | 9 | 4 |
| 5 | 9 | 4 | 4 |
Input
Programs can take input in many forms: touch, sound, images or typed text. In Java, the Scanner class is one way to read text typed at the keyboard:
Scanner input = new Scanner(System.in);
int age = input.nextInt();
System.out.println("Next year you'll be " + (age + 1));
If the user types 16, this prints Next year you'll be 17. The parentheses around age + 1 make Java add before joining the number to the string.
The exam won't test any particular way of getting input from a user, so you don't need to memorize keyboard input. You will use Scanner again in 4.6 to read text files, and that is tested.
Not on the exam
Assignments inside other expressions, like a = b = 4;, won't appear. Each assignment is its own statement.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Tracing assignments
What does this code print?
int x = 5; int y = x * 2; x = y - 1; y = y + x; System.out.println(x + " " + y);Show the solutionHide the solution
- Step 1: Line 1:
xis 5. - Step 2: Line 2:
x * 2is 10, soyis 10. - Step 3: Line 3: the right side uses the current
y:10 - 1is 9. Nowxis 9. The old value 5 is gone. - Step 4: Line 4:
y + xis10 + 9, which is 19, soyis 19. - Step 5: Line 5 prints
x, a space, theny.
Answer: It prints
9 19. - Step 1: Line 1:
- Example 2
A swap that doesn't swap
A student tries to swap
aandbwithout a temporary variable. What is printed?int a = 4; int b = 9; a = b; b = a; System.out.println(a + " " + b);Show the solutionHide the solution
- Step 1: After line 2,
ais 4 andbis 9. - Step 2: Line 3 stores
b's value ina, soais 9. The original 4 is now lost, because nothing else held it. - Step 3: Line 4 stores
a's value, which is now 9, inb. Sobis still 9. - Step 4: To swap correctly, save one value first:
int temp = a; a = b; b = temp;.
Answer: It prints
9 9. This is a logic error: the code runs but doesn't swap. - Step 1: After line 2,
Common mistakes
- Reading
=as "equals". It's an action: compute the right side, then store it on the left. - Using an old value after it has been overwritten. When you trace, cross out the old value in your table as soon as a variable changes.
- Forgetting to initialize a variable such as a running total before adding to it.
int total;followed bytotal += 5;won't compile; start withint total = 0;.
On the exam
- Many multiple-choice questions are just a sequence of assignments followed by a print. Use a trace table instead of keeping values in your head.
- In free-response code, initialize every counter and total before a loop, usually to 0.
Connected topics
Videos
Check yourself
3 questions on 1.4 Assignment Statements and Input. Pick an answer to see if you got it, and why.
Consider the following code segment.int a = 3;
int b = 5;
a = b;
b = a;
System.out.println(a + " " + b);What is printed as a result of executing the code segment?
Which of the following code segments causes a compile-time error?
Consider the following code segment.int n = 4;
n = n * n - n;
n = n / 3;
System.out.println(n);What is printed as a result of executing the code segment?
0 of 3 answered