# AP® Computer Science A 1.4 Assignment Statements and Input

Source: https://lockincs.com/ap-csa/unit-1/1-4-assignment-statements-and-input/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.4.A, 1.4.A.1, 1.4.A.2, 1.4.A.3, 1.4.B, 1.4.B.1

The assignment operator = evaluates the expression on the right and stores the value in the variable on the left. Every variable must be assigned a value of a compatible type before it is used, and a reference variable can hold a new object or null. Input can come in many forms; the Scanner class is one way to read text from the keyboard.

## What you will learn

- 1.4.A Develop code for assignment statements with expressions and determine the value that is stored in the variable as a result of these statements.
- 1.4.B Develop code to read input.

## 1\. Giving a variable its first value

Every variable must be assigned a value before it can be used in an expression, and that value must be of a compatible data type. A variable is **initialized** the first time it is assigned a value 1.4.A.1.

```
// does not compile
int n;
System.out.println(n);
```

Real view: the compiler rejects the code because `n` has no value yet.

```
error: variable n might not have been initialized
```

Declaring and initializing can happen in one statement or in two:

```
int lives = 3;     // declared and initialized together
int bonus;         // declared only
bonus = 2;         // initialized here
System.out.println(lives + bonus);
```

Output:

```
5
```

A reference variable can be assigned a new object, or `null` if there is no object. The literal `null` is a special value that means a reference is not associated with any object 1.4.A.1.

```
String title = "Chess Club";
String nickname = null;      // no String object yet
System.out.println(title);
System.out.println(nickname);
```

Output:

```
Chess Club
null
```

Printing a `null` variable prints the word `null`. Calling a method on it is different: that throws a `NullPointerException` (topic 1.14).

## 2\. The assignment operator

The assignment operator `=` gives a variable its first value (initializes it) or replaces the value it holds. Java stores the value of the expression on the right side in the variable on the left side 1.4.A.2.

Java evaluates the right side first, using the current values, and then stores the result. So `c = c + 1;` is not an equation: it adds 1 to `c`.

```
int c = 4;
c = c + 1;
System.out.println(c);
```

Output:

```
5
```

Assigning a primitive copies the value. After `int b = a;`, changing `a` does not change `b`.

```
int a = 10;
int b = a;   // b gets a copy of 10
a = 99;
System.out.println(a + " " + b);
```

Output:

```
99 10
```

For a reference variable, the value copied is the reference, so both variables then refer to the same object (see aliasing in topic 1.14).

Outside the course: assignments inside expressions, such as `a = b = 4;`.

## 3\. Expressions produce one value

During execution, an **expression** is evaluated to produce a single value, and that value has a type based on the evaluation 1.4.A.3. `a + 4` is an expression: it produces a value. A simple statement, such as an assignment or a method call, is a complete instruction that ends in `;`, such as `int b = a + 4;`.

```
int a = 7;
double half = a / 2.0;  // the expression a / 2.0 has type double
int whole = a / 2;      // the expression a / 2 has type int
System.out.println(half + " " + whole);
```

Output:

```
3.5 3
```

The type of the expression must be compatible with the variable. `a / 2.0` is a `double`, so it could not be stored in an `int` variable without a cast (topic 1.5).

## 4\. Trace tables

A trace table follows the values step by step. Each row runs one line of code and shows the values stored afterwards. A blank cell means the variable does not exist yet.

```
int a = 3;
int b = a + 4;
a = b * 2;
b = b - a;
System.out.println(a + " " + b);
```

| Line | Code | a | b |
| --- | --- | --- | --- |
| 1 | `int a = 3;` | 3 |  |
| 2 | `int b = a + 4;` | 3 | 7 |
| 3 | `a = b * 2;` | 14 | 7 |
| 4 | `b = b - a;` | 14 | \-7 |

Output:

```
14 -7
```

**Watch out:** line 4 uses the NEW value of `a` (14, not 3). Update your trace table after every line; never use a value from memory.

## 5\. Input

Input can come in a variety of forms, such as tactile (touch), audio, visual or text. The `Scanner` class is one way to get text input from the keyboard 1.4.B.1. No specific form of user input is tested on the AP® exam, so you only need the idea.

```
import java.util.Scanner;

public class Greeting
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Name?");
        String name = input.nextLine();
        System.out.println("Age?");
        int age = input.nextInt();
        System.out.println("Hi " + name + ", next year you will be " + (age + 1));
    }
}
```

Real view: the console when the user types `Ana` and `16` (the typed lines are the user’s input).

```
Name?
Ana
Age?
16
Hi Ana, next year you will be 17
```

## Common mix-ups

- Not true: = means "is equal to", like in math. In Java, = means "store the value on the right in the variable on the left". So c = c + 1; is not an impossible equation: it adds 1 to c.
- Not true: After int b = a; changing a also changes b. Assigning a primitive copies the value. b keeps its own copy, so a later change to a does not affect b.
- Not true: A declared variable starts at 0, so it can be printed right away. A local variable must be assigned a value before it is used. int n; followed by System.out.println(n); does not compile.

## In 30 seconds

- A variable must be assigned a value of a compatible type before it is used; the first assignment initializes it.
- A reference variable can be assigned a new object, or null when there is no object.
- = evaluates the right side using current values, then stores the result in the variable on the left.
- An expression is evaluated to one value, and that value has a type.
- Input can be tactile, audio, visual or text; the Scanner class is one way to read keyboard text.

## Checkpoint: 4 practice questions

### Question 1

What is printed?

```java
int x = 4;
int y = x * 2;
x = y - x;
y = x + y;
System.out.println(x + " " + y);
```

- A. `4 8`
- B. `8 12`
- C. `4 16`
- D. `4 12`

**Answer: D.** x 4, y 8; x = 8 - 4 = 4; y = 4 + 8 = 12. A forgets the last line. B takes x as 8 (the value of y) instead of 8 - 4. C adds y to itself (8 + 8) instead of using x.

### Question 2

Which statement best describes the `Scanner` class?

- A. It displays text on the screen.
- B. It is one way to obtain text input from the keyboard.
- C. It is the only way a program can receive input.
- D. It is the only class that can read text.

**Answer: B.** `Scanner` is one way to read keyboard text; input can come in many forms (tactile, audio, visual or text). A describes output, done by `System.out`. C and D are wrong because Scanner is only one of many ways to get input.

### Question 3

The following code segment is intended to print 100, but it does not compile. Which of the following best explains why?

```java
int steps;
steps = steps + 100;
System.out.println(steps);
```

- A. `steps` has no value when it is first used.
- B. The sum `steps + 100` is outside the int range.
- C. An int variable cannot be assigned a second time.
- D. `steps` must be a double to be added to 100.

**Answer: A.** Every variable must be assigned a value before it is used in an expression. `steps` is declared but not initialized, so `steps + 100` does not compile. B is wrong because 100 is far inside the int range. C is wrong because variables can be assigned many times. D is wrong because an int can be added to an int.

### Question 4

Consider the following code segment. What is printed?

```java
int a = 6;
int b = a;
a = a * 3;
b = b + a;
System.out.println(a + " " + b);
```

- A. `18 18`
- B. `18 36`
- C. `18 24`
- D. `18 12`

**Answer: C.** `b = a` copies the value 6, so b is 6. Then a becomes 18 and b becomes 6 + 18 = 24. A treats b as linked to a. B adds the new a to itself, as if b were 18. D adds the old value of a (6) instead of the current value 18.

## Questions students ask

**What does = mean in Java?**

= is the assignment operator. Java evaluates the expression on the right, then stores that value in the variable on the left. It does not test whether two values are equal.

**What does it mean to initialize a variable?**

A variable is initialized the first time it is assigned a value, as in int lives = 3; or lives = 3; after int lives;. A variable must be initialized before it is used in an expression.

**What is null in Java?**

null is a special literal that means a reference variable is not associated with any object. Plant r = null; declares a variable that can refer to a Plant but refers to none yet.

**Is Scanner tested on the AP® Computer Science A exam?**

No specific form of user input is tested. You only need to know that input can come in many forms and that the Scanner class is one way to get text input from the keyboard.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
