01 / 21
\(\texttt{x =}\)
\(5\)

Assignment Statements
and Input

Master how Java stores values in variables using assignment statements, and learn to capture user input with the Scanner class.

AP Computer Science A

What Is a Variable?

A variable is a named container in memory that holds a single value at a time. Think of it as a labeled box: the label is the variable's name, and the contents are its value.

score
\(95\)
name
\(\texttt{"Ada"}\)
int String

Declaring Variables in Java

A declaration tells the compiler the variable's type and name. You may also initialize it with a starting value in the same statement.

// Declaration only int age; // Declaration with initialization double gpa = 3.85; String greeting = "Hello";
int
Whole numbers: \( -2, 0, 42 \)
double
Decimals: \( 3.14, -0.5 \)
boolean
True or false only

Why Assignment Matters

Almost every Java program stores, updates, and retrieves data. Without assignment statements, programs could not remember anything between lines of code.

1
Store initial values from the user or from calculations.
2
Update values as the program runs: counters, totals, flags.
3
Retrieve values to display results or make decisions.

The Assignment Statement

The assignment operator \(\texttt{=}\) copies the value on the right side into the variable on the left side. It does not mean "equals" in the math sense.

variable
\(\texttt{x}\)
\(=\)
expression
\(10 + 5\)
Right side is evaluated first, then stored in left side
int x = 10 + 5; // x now holds 15

Reference assignment as an analogy

Copying an object reference does not copy the mutable object. Reassigning a String reference does not alter the original reference; the apparatus is an analogy, not a literal Java heap.

Java reference analogy: a and b share a mutable object, so a mutation through b is visible through a. Reassigning t from "oak" to "elm" leaves s referring to "oak". The apparatus is an analogy, not a literal Java heap.
Java reference analogy: a and b share a mutable object, so a mutation through b is visible through a. Reassigning t from "oak" to "elm" leaves s referring to "oak". The apparatus is an analogy, not a literal Java heap.

Re-assignment: Overwriting Values

When you assign a new value to an existing variable, the old value is destroyed. The variable only ever holds one value at a time.

int count = 3; count = 7; // old value 3 is gone count = count + 1; // count is now 8
Step 1
\(3\)
Step 2
\(7\)
Step 3
\(8\)
count count count
\(\texttt{= 3}\)
\(\texttt{= 7}\)
\(\texttt{= count + 1}\)

\(\texttt{=}\) versus \(\texttt{==}\)

The single equals sign and the double equals sign do completely different things in Java. Confusing them is one of the most common bugs.

\(\texttt{=}\) (Assignment) \(\texttt{==}\) (Comparison)
Purpose Stores a value Tests if two values are equal
Returns The assigned value A boolean (true/false)
Example \(\texttt{x = 5}\) \(\texttt{x == 5}\)
Used in Statements Conditions (if, while)

Compound Assignment Operators

Java provides shorthand operators that combine arithmetic and assignment into a single step. These are tested on the AP exam.

Shorthand Equivalent Result (\(\texttt{x}\) starts at 10)
\(\texttt{x += 3}\) \(\texttt{x = x + 3}\) 13
\(\texttt{x -= 4}\) \(\texttt{x = x - 4}\) 6
\(\texttt{x *= 2}\) \(\texttt{x = x * 2}\) 20
\(\texttt{x /= 5}\) \(\texttt{x = x / 5}\) 2
\(\texttt{x \%= 3}\) \(\texttt{x = x \% 3}\) 1

Increment and Decrement

The \(\texttt{++}\) and \(\texttt{--}\) operators add or subtract 1. They are the most compact form of re-assignment.

int lives = 3; lives++; // lives is now 4 (same as lives = lives + 1) lives--; // lives is now 3 again

User Input with Scanner

The Scanner class reads input from the keyboard. You must import it and create an object before using it.

import java.util.Scanner; Scanner input = new Scanner(System.in); System.out.print("Enter your age: "); int age = input.nextInt();

Key Scanner Methods

nextInt()
Reads the next integer from input
nextDouble()
Reads the next decimal number
nextLine()
Reads the entire line as a String
double price = input.nextDouble(); input.nextLine(); // consume leftover newline String item = input.nextLine();

How to Apply Assignment

1
Identify the type of data you need to store (int, double, String, boolean).
2
Declare the variable with its type and a meaningful name.
3
Initialize the variable using \(\texttt{=}\) and an expression or literal.
4
Update as needed with \(\texttt{=}\), \(\texttt{+=}\), \(\texttt{-=}\), \(\texttt{++}\), or \(\texttt{--}\) operators.
5
Trace the variable's value after each statement to verify correctness.

Example 1: Simple Assignment

Trace the value of each variable after every line.

int a = 5; int b = 10; a = b; b = 20;
\(\texttt{a}\)
\(\texttt{b}\)
After line
\(5\)
- int a = 5;
\(5\)
\(10\)
int b = 10;
\(10\)
\(10\)
a = b; ← a gets b's value
\(10\)
\(20\)
b = 20; ← a stays 10

Two collaborators work on one shared model

Two collaborators adjust one blue articulated model while a second model stands apart.Java assignment copies a value. For primitive variables this is the primitive value; for object variables it is a reference to an object.

Assignment copies a value

Primitive variables x and y separate after reassignment; String references a and b point to different immutable strings.After x is changed to 9, y keeps its copied value of 42. For String variables, assignment copies a reference: reassigning a to "world" leaves b referring to the unchanged "hello" object.

Example 2: Swapping Two Variables

You cannot swap two variables directly. A temporary variable is required to hold one value during the swap.

int x = 3, y = 7; int temp = x; // temp = 3 x = y; // x = 7 y = temp; // y = 3
x
\(7\)
y
\(3\)
temp
\(3\)
After swap: \(\texttt{x = 7}\), \(\texttt{y = 3}\)

Example 3: Input and Calculation

A complete program that reads two numbers from the user, calculates their average, and prints the result.

import java.util.Scanner; public class Average { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter num1: "); double num1 = sc.nextDouble(); System.out.print("Enter num2: "); double num2 = sc.nextDouble(); double avg = (num1 + num2) / 2.0; System.out.println("Average: " + avg); } }

Common Mistakes

✕ Using an uninitialized variable
Declaring \(\texttt{int x;}\) then using \(\texttt{x}\) before assigning a value causes a compile error.
✕ Confusing \(\texttt{=}\) and \(\texttt{==}\)
Writing \(\texttt{if (x = 5)}\) in Java causes a compile error because the assignment returns an \(\texttt{int}\), not a \(\texttt{boolean}\). Use \(\texttt{==}\) for comparison.
✕ Integer division surprise
\(\texttt{int / int}\) truncates the decimal. \(7 / 2\) gives \(3\), not 3.5. Use a double operand.

Question 1 of 5

What is the value of \(\texttt{y}\) after the following code runs?

int x = 4; int y = x + 3; x = 10;

Question 2 of 5

Which line of code correctly declares and initializes a double variable?

Question 3 of 5

What does \(\texttt{x \%\%= 4}\) do if \(\texttt{x}\) is currently \(15\)?

Question 4 of 5

To swap the values of variables \(\texttt{a}\) and \(\texttt{b}\), what is the minimum number of assignment statements needed?

Question 5 of 5

What is printed by this code?

int n = 7; n += 3; n *= 2; System.out.println(n);