6m left·0%
Reading Time: 6 min
Last Updated: July 6, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: July 6, 2026
Main Ideas: 5

Topic 1.4 Notes – Assignment Statements and Input

Verified for 2027 AP® Computer Science A Exam
Read aloud
Assignment statements store values in variables by evaluating the right side first. This guide covers how expressions evaluate, how types affect results, and common pitfalls with initialization, null references, and Scanner input.

1. What Assignment Statements Do

An assignment statement stores a value in a variable using =. The entire right side is evaluated first, then that final value is stored in the variable on the left.

General form:

variable = expression;

Here’s what that means in action:

int points = 10;
points = points + 4;
  • Java looks at points + 4 first.
  • If points was 10, that becomes 14.
  • Then 14 is stored back into points.

This diagram walks through that process step by step.

Evaluating the right side before assignment

Two rules you must remember:

  • Every variable must be assigned before it’s used.
    Using an uninitialized local variable causes a compile-time error.
  • The value assigned must be a compatible type.
    int x = 3.5; does not compile.

The first time you give a variable a value, that’s called initialization.

Reference types (like String) can store:

  • A reference to an object
  • Or null

null means the variable refers to no object.

2. How Expressions Evaluate

An expression produces a single value. That value has a type.

Order of Operations

Java follows normal math rules:

  1. Parentheses
  2. Multiplication and division
  3. Addition and subtraction
  4. Assignment happens last

Example:

int result = 8 - 2 * 3;

Multiplication happens first → 2 * 3 = 6
Then subtraction → 8 - 6 = 2

If you add parentheses:

int result = (8 - 2) * 3;

Now subtraction happens first → 6 * 3 = 18

Here’s a fuller operator precedence chart you can use for reference. Focus on the top-to-bottom order.

Study guide illustration

Java operator precedence and associativity

The Type of the Expression Matters

The final value has a type based on how it evaluates.

  • int / int → integer division
    7 / 2 becomes 3
  • If one value is double, result is double
    7 / 2.0 becomes 3.5
  • If one operand is a String, + becomes concatenation
int num = 5;
String text = "Score: " + num;  // "Score: 5"

On multiple choice, they love mixing integer division and strings in the same expression. Slow down and evaluate carefully.

3. Initialization, Reassignment, and null

Primitive Variables

Primitives (int, double, boolean) must be assigned before use.

int total = 0;   // initialized
total = total + 5;  // reassigned

Reassignment simply replaces the old value.

If you try:

int total;
int sum = total + 3;   // compile error

Java stops you immediately.

Reference Variables and null

Reference variables store memory addresses, not actual data.

String name = "Jordan";

That variable refers to a String object.

You can also do:

String name = null;

Now name refers to nothing.

Calling a method on null causes a NullPointerException:

name.length();  // crash if name is null

Safer pattern when possible:

String name = "";

Empty string is still a real object. Length is 0, but no crash.

On code-tracing questions, always check whether a reference might be null before a method call.

4. Reading Input with Scanner

Programs can get input in many ways. The Scanner class is one way to read text input.

Basic pattern:

Scanner scan = new Scanner(System.in);

int age = scan.nextInt();
double gpa = scan.nextDouble();
String word = scan.next();
String line = scan.nextLine();

scan.close();

Each next...() method call:

  • Produces a value
  • That value must be assigned to a variable

So this fits directly with assignment statements.

The nextInt and nextLine Issue

Common bug:

int age = scan.nextInt();
String name = scan.nextLine();  // often reads empty string

Why? nextInt() leaves the newline character behind.

Fix:

int age = scan.nextInt();
scan.nextLine();  // clear leftover newline
String name = scan.nextLine();

You won’t see detailed keyboard input scenarios on the AP exam, but understanding how assignment works with input is still important, especially when reading from files later in the course.

5. Common Assignment Mistakes

Confusing = and ==

  • = assigns
  • == compares

This shows up constantly in quizzes.

Forgetting the Right Side Evaluates First

If you see:

x = x + 1;

It does not mean “x equals x.”
It means “compute x + 1 using the old value, then store it.”

Chained or Embedded Assignments

Things like:

a = b = 4;

are outside the scope of the AP exam. Keep assignments simple and readable.

Key Takeaways

The right side of an assignment always evaluates completely before the value is stored on the left.
A variable must be assigned before it is used in any expression.
The type of an expression determines what can be stored in the variable.
Integer division (int / int) truncates the decimal.
Reference variables can store an object or null, and calling a method on null causes a runtime error.
Every Scanner input method returns a value that must be assigned to a variable.
When mixing nextInt() and nextLine(), clear the leftover newline before reading a full line.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining