Skip to main content

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
  • null
  • Scanner

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 lineabtemp
14––
249–
3494
4994
5944

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.

  1. 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 solution
    1. Step 1: Line 1: x is 5.
    2. Step 2: Line 2: x * 2 is 10, so y is 10.
    3. Step 3: Line 3: the right side uses the current y: 10 - 1 is 9. Now x is 9. The old value 5 is gone.
    4. Step 4: Line 4: y + x is 10 + 9, which is 19, so y is 19.
    5. Step 5: Line 5 prints x, a space, then y.

    Answer: It prints 9 19.

  2. Example 2

    A swap that doesn't swap

    A student tries to swap a and b without a temporary variable. What is printed?int a = 4; int b = 9; a = b; b = a; System.out.println(a + " " + b);

    Show the solution
    1. Step 1: After line 2, a is 4 and b is 9.
    2. Step 2: Line 3 stores b's value in a, so a is 9. The original 4 is now lost, because nothing else held it.
    3. Step 3: Line 4 stores a's value, which is now 9, in b. So b is still 9.
    4. 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.

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 by total += 5; won't compile; start with int 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

  • AP Computer Science A - Topic 1.4 - Part 1: Assignment Statements and Input

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Assignment Statements

    CodeHSWatch on YouTube (opens in a new tab)

  • Declaring and Initializing a Variable (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 1.4 - Part 2: Assignment Statements and Input

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • How to accept user input in Java ⌨️【8 minutes】

    Bro CodeWatch on YouTube (opens in a new tab)

Check yourself

3 questions on 1.4 Assignment Statements and Input. Pick an answer to see if you got it, and why.

Question 1 of 3

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?

Question 2 of 3

Which of the following code segments causes a compile-time error?

Question 3 of 3

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