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

Topic 1.2 Notes – Variables and Data Types

Verified for 2027 AP® Computer Science A Exam
Read aloud
Variables and data types are the foundation of every Java program. In AP Computer Science A, you’ll work with a small, focused set of types and learn how to choose and declare variables correctly. This topic is about understanding what a type really means and how variables store data while your program runs.

1. What Data Types and Variables Are

A data type is a set of values and the operations you’re allowed to perform on those values.

For AP CSA, the core primitive types are:

  • int
    • Values: whole numbers like −3, 0, 42
    • Operations: arithmetic such as +, -, *, /, %
  • double
    • Values: real numbers with decimals like 3.14 or −0.5
    • Operations: decimal arithmetic
  • boolean
    • Values: true or false
    • Operations: logical expressions like &&, ||, !

A variable is a named storage location that:

  • Has a type
  • Has a name
  • Holds a value that can change while the program runs

For primitive types, the variable directly stores the actual value. Think of each variable as its own labeled box that contains the value itself.

When you see int count = 5;, that 5 is literally stored inside count.

Keep this mental model as we compare primitive and reference types.

2. Primitive vs Reference Types

Java splits data types into primitive and reference types.

Primitive Types (AP Scope)

You only need to know three:

  • int
  • double
  • boolean

Other primitives like long, float, char, short, and byte are outside AP scope.

Key features:

  • Store actual values directly.
  • Written in lowercase.
  • Used for numbers and logical flags.

Reference Types

A reference type stores a reference to an object in memory, not the object itself.

Examples:

  • String
  • Scanner
  • Any class you create

The diagram below shows the difference. A primitive variable holds its value directly. A reference variable holds a pointer to a separate object.

Primitive vs. reference variable memory model

For this topic, you mostly need to recognize the difference conceptually. If it’s a number or true/false, it’s primitive. If it’s an object like a String, it’s reference.

3. Declaring and Initializing Variables

A declaration tells Java the type and name.

int points;
double price;
boolean isReady;

Format:

type variableName;

Initialization gives the variable a value.

int points = 10;
double taxRate = 0.07;
boolean finished = false;

You can declare and initialize together, or separately.

What you cannot do:

int bonus;
System.out.println(bonus); // compile error

Java requires local variables to be assigned a value before use. On quizzes, a common trick is initializing a variable in only one branch of an if statement and then using it after.

4. Choosing the Correct Data Type

When reading a problem, the wording usually tells you the type.

  • “Count how many” → int
  • “Average” or “percentage” → double
  • “Returns true if…” → boolean
  • Loop counters and array indices → always int

Examples:

int totalStudents = 28;
double classAverage = 91.4;
boolean isPassing = true;

If decimals might appear at any point, use double. Even if the starting value is 5, if it could become 5.5 later, it should be a double.

5. Type Compatibility and Common Traps

Type Mismatch

Illegal:

int x = 4.8;      // double into int
boolean flag = 1; // number into boolean

Legal:

double y = 7;  // int automatically promoted to double

Java allows widening (int → double) but not narrowing automatically.

Explicit cast:

double price = 12.99;
int dollars = (int) price;  // becomes 12

Casting truncates. It never rounds.

Integer Division

If both operands are int, the result is int.

7 / 4   →   1

This causes subtle bugs:

int correct = 18;
int total = 25;
double percent = correct / total * 100;  // wrong

correct / total evaluates first using integer division.

Fix it:

double percent = (double) correct / total * 100;

Or:

double percent = correct / 25.0 * 100;

Any time you see division in a multiple choice question, check the types immediately.

Key Takeaways

A data type defines both the allowed values and the operations you can perform on them.
For AP CSA primitives, you only need int, double, and boolean.
Primitive variables store their actual value directly.
Local variables must be initialized before use or you get a compile-time error.
If both operands are int, division produces an int.
Java automatically converts int to double, but never double to int without a cast.
Casting from double to int truncates the decimal part, it does not round.

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