TLDR
Assignment statements use the = operator to store a value in a variable: Java evaluates the expression on the right first, then puts that result into the variable on the left. A variable must be initialized with a compatible type before you use it in an expression, and reference variables can hold an object or null. The Scanner class is named as one way to read text input, but the AP exam does not test specific keyboard input code.

Why This Matters for the AP Computer Science A Exam
Assignment is the action behind almost every line of code you trace or write. On the multiple-choice section, you constantly evaluate expressions and track how a variable's value changes line by line, so knowing that the right side is fully evaluated before it is stored keeps your traces accurate. In free-response code writing, you set up counters, running totals, and updated values using assignment, and you assign values to instance variables inside constructors and methods. Getting initialization and compatible types right is what makes the rest of your code run as intended.
Key Takeaways
- The assignment operator
=stores the value of the expression on the right into the variable on the left. - A variable must be assigned a value before it can be used in an expression, and that value must come from a compatible data type.
- A variable is initialized the first time it is assigned a value.
- An expression evaluates to a single value, and that value has a type based on how the expression is evaluated.
- Reference variables can be assigned a new object or
null, wherenullmeans the reference is not associated with any object. - The Scanner class is one way to obtain text input from the keyboard, but specific input code is not tested on the exam.
How Assignment Works
The right side of an assignment is an expression. Java evaluates that whole expression down to one value, then stores it in the variable named on the left. This order matters most when the same variable appears on both sides.
</>Javaint count = 0; // initialization: declare and assign in one statement count = count + 1; // evaluate count + 1 (0 + 1), then store 1 back into count
In the second line, Java reads the current value of count (0), computes 0 + 1, and only then assigns 1 back to count.
Initialization vs. Assignment
A variable is initialized the first time it gets a value. After that, assigning again just changes the stored value.
</>Javaint score; // declared, not yet initialized score = 75; // initialized here (first assignment) score = 90; // reassigned to a new value
Every variable must have a value before you use it in an expression, and that value has to be a compatible type. You cannot store a double value directly into an int variable without handling the type difference (you will see casting in a later topic).
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. So 5 + 3 * 2 becomes the single int value 11, and that value is what gets assigned.
</>Javaint result = 5 + 3 * 2; // result = 11 (multiplication before addition) int complex = (10 + 5) * (3 - 1); // complex = 30 (parentheses change grouping)
Reference Types and null
Reference variables hold an object reference, not the object itself. You can assign a reference variable a new object or the literal null, which means it is not pointing at any object.
</>JavaString name = "Sam"; // refers to a String object String other = null; // refers to no object
Calling a method on a null reference causes a problem at run time, so make sure a reference actually points to an object before you use it.
Reading Input
Input can come in many forms, such as text, audio, or visual. The Scanner class is named as one way to obtain text input from the keyboard. You do not need to write specific keyboard input code for the AP exam, so focus your study time on assignment, expression evaluation, and tracing values rather than memorizing input methods.
How to Use This on the AP Computer Science A Exam
Code Tracing
When you trace assignment statements, evaluate the right side completely before changing the variable. A simple trace table helps: list each variable as a column and update its value on the line where it changes.
</>Javaint x = 5; int y = x * 2 + 3; // y = 13 x = x + 1; // x = 6, but y stays 13
Notice that changing x after y is assigned does not change y. Once an expression is evaluated and stored, it does not update on its own.
Free Response
When a problem asks you to count, total, or accumulate, declare and initialize a variable first, then update it with assignment.
</>Javaint count = 0; int total = 0; // inside a loop you would update them: count = count + 1; total = total + value;
Inside constructors and methods, you assign values to instance variables. When a parameter has the same name as an instance variable, use this to be clear about which one you mean (you will use this pattern more in the class design unit).
Common Trap
Initialize variables when you can. Forgetting to give a local variable a value before using it in an expression is a frequent mistake, and so is assuming a later change to one variable updates another that was already computed.
Common Misconceptions
- The right side is evaluated first, not the left. In
count = count + 1, Java computescount + 1using the old value, then stores the result. - Assigning a new value to one variable does not update another variable that was already calculated from it. Earlier results are fixed once stored.
nullis not the same as an empty value like""or0. Anullreference points to no object at all, and using it can cause a run-time error.- Declaring a variable is not the same as initializing it. A variable is only initialized when it first receives a value, and you must do that before using it in an expression.
- Types must be compatible. You cannot freely store a
doublevalue into anintvariable without addressing the type difference. - Knowing the Scanner class by name is enough for this topic. The exam does not require you to write specific keyboard input code.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
assignment operator | The = symbol that allows a program to store the value of an expression on the right into a variable on the left. |
assignment statement | A statement that stores a value in a variable using the assignment operator or a compound assignment operator. |
data type | A classification that specifies what kind of value a variable can store and what operations can be performed on it. |
expression | A combination of values, variables, and operators that is evaluated to produce a single value. |
initialization | The first assignment of a value to a variable. |
input | Data or information provided to a program, which can come from various sources such as keyboard, files, or user interactions. |
null | A special value indicating that a reference variable does not currently reference any object. |
reference type | A data type that holds a reference (memory address) to an object rather than storing the object's value directly. |
Scanner class | A Java class used to obtain and parse text input from the keyboard or other input sources. |
text input | Data entered by a user in the form of characters and strings, typically from a keyboard. |
variable | A named storage location in a program that holds a value of a specific data type; the value can change while the program is running. |
Frequently Asked Questions
What is an assignment statement in Java?
An assignment statement stores the value of an expression in a variable. Java evaluates the expression on the right side of the = operator first, then stores that result in the variable on the left.
What does the = operator do in AP CSA?
The = operator initializes or changes the value stored in a variable. It is assignment, not a mathematical equality statement.
What does it mean to initialize a variable?
A variable is initialized the first time it is assigned a value. In Java, a local variable must be initialized before it can be used in an expression.
Why does Java evaluate the right side first?
In an assignment such as count = count + 1, Java uses the current value of count to compute count + 1, then stores the new result back into count. This is why tracing line by line matters.
What does null mean for a reference variable?
null means the reference variable is not associated with any object. Calling a method on a null reference causes a run-time error, so you should only use object methods when the reference points to an object.
Do I need to know Scanner input code for the AP CSA exam?
You should know that Scanner is one way to get keyboard text input, but the AP CSA exam does not test any specific form of user input code. Focus on assignment, expression values, and tracing.