A variable is a named box in the computer's memory that holds one value at a time. Java is statically typed: you state the type of the box once, when you declare it, and that box can only ever hold that kind of value.
The three primitive types the exam cares about
Java has eight primitive types, but AP Computer Science A focuses on just three. Memorize this table — every lesson leans on it.
| Type | Holds | Example value |
|---|---|---|
| int | whole numbers (no decimal point) | 42, -7, 0 |
| double | numbers with a decimal point | 3.14, -0.5, 9.0 |
| boolean | a truth value, nothing else | true, false |
The three primitive types you must know cold.
Declare, then initialize
Declaring a variable creates the box and names it. Initializing puts the first value in it. You can do both in one line.
int score; // declare an empty int box named score
score = 90; // initialize it (put 90 in the box)
double price = 3.99; // declare and initialize together
boolean passed = true; // a boolean is true or false only
[Diagram — see the figure in the print workbook.]
Naming rules and the camelCase convention
A name may use letters, digits, and underscores, but cannot start with a digit and cannot be a keyword (like int or new). By convention, variable names use camelCase: start lowercase, capitalize each later word — finalScore, numStudents, isReady.
A variable holds exactly one value. Assigning a new value replaces the old one — the box does not remember its past.
Worked example 1
Declare three variables and print them.
int age = 15;
double gpa = 3.8;
boolean enrolled = true;
System.out.println(age);
System.out.println(gpa);
System.out.println(enrolled);
Output:
15
3.8
true
Each println prints the current value of the box, then moves to a new line.
Worked example 2
Why won't this compile?
int count = 5.0;
5.0 is a double literal (it has a decimal point). An int box cannot hold a double. Use int count = 5; or double count = 5.0;.
Worked example 3
What prints?
int x = 10;
x = 25;
x = x + 1;
System.out.println(x);
Output:
26
The box x is overwritten each time. After x = 25 it holds 25; then x = x + 1 means “take the current value (25), add 1, store 26 back.”
Worked example 4
Use one variable to initialize another.
int a = 3;
int b = a + 4;
System.out.println(b);
Output:
7
When b is created, Java first evaluates a + 4 using the current value of a (3), giving 7.
Worked example 5
Pick the correct type for each value.
For each value, which primitive type fits best? 72, 98.6, false, -4.
72 → int; 98.6 → double; false → boolean; -4 → int (negatives are still whole numbers).
Common trap: boolean is not a number
In some languages 0 means false and 1 means true. Not in Java. A boolean can only be the literals true or false. Writing boolean done = 1; is a compile-time error.
Hack: Name the box before you fill it
When you read code, draw a tiny box for each variable and write its current value inside. Cross out and rewrite the value on every assignment. Tracing memory by hand turns confusing problems into bookkeeping — and that is exactly how the multiple-choice section tests you.
Mnemonic for the big three: “I'd Double-check Booleans” → Int, Double, Boolean.