Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.2 Variables and Primitive Data Types

1.2 Variables and Primitive Data Types

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

TLDR

A variable is a named storage location that holds a value, and the value can change while your program runs. In AP Computer Science A, you work with three primitive types (int, double, boolean) plus reference types like String, and you pick the right type based on what kind of data you need to store. Every variable has a name and a data type, and you must give a variable a value before you use it.

Why This Matters for the AP Computer Science A Exam

This topic is foundational, which means it shows up everywhere later. The multiple-choice section tests whether you can read code and pick the correct data type, spot type mismatches, and predict what value a variable holds. Free-response code writing also depends on choosing the right type, since picking int when you needed double (or the reverse) can quietly change your output.

You are being asked to do two things here: identify the most appropriate data type category for a given situation, and write code that declares variables to store numbers and Boolean values. Both skills carry directly into tracing code and writing algorithms in every unit that follows.

Key Takeaways

  • A data type is a set of values plus the operations allowed on those values, and types are either primitive or reference.
  • The three primitive types in this course are int (integers), double (real numbers), and boolean (true or false).
  • A reference type defines objects that are not primitive types; a primitive variable holds the value directly.
  • Every variable has a name and an associated data type, and a primitive variable holds a value of that type.
  • A variable's value can change while the program runs, but its declared type stays the same.
  • The five other primitive types (long, short, byte, float, char) are not part of this course or exam.

Core Concepts

Primitive vs Reference Types

A data type is a set of values and the operations you can perform on those values. Java sorts data types into two categories:

  • Primitive types store a simple value directly in the variable. The primitive types in this course cover numbers and true/false values.
  • Reference types define objects that are not primitive types. A reference variable holds a reference to an object rather than the object's data directly.

A quick clue: primitive types are written in all lowercase (int, double, boolean), while reference types like String start with a capital letter.

The Three Primitive Types You Need

For this course, focus on exactly three primitive types:

  • int stores an integer (a whole number), positive or negative. Use it for counting, indexing, or any value that should not have a decimal part.
  • double stores a real number (a value that can have decimals). Use it for measurements, averages, percentages, or anything that might produce fractions.
  • boolean stores either true or false. Use it for yes/no conditions, flags, or tracking whether something has happened.

The other five primitive types (long, short, byte, float, and char) are outside the scope of the course and exam, so you do not need them.

Declaring Variables

A variable is a storage location that holds a value, and that value can change while the program runs. Every variable has two parts: a name and an associated data type. Declaring a variable tells Java the type and name; you can also give it a starting value at the same time.

</>Java
// Declaration only
int score;
double price;
boolean isGameOver;

// Declaration with a starting value
int lives = 3;
double tax = 0.08;
boolean hasKey = false;

A primitive variable holds a primitive value from its type, so an int variable holds an integer, a double variable holds a real number, and a boolean variable holds true or false.

Choosing the Right Type

Picking the type comes down to what the data represents:

  • Counting items, tracking a level, or storing an index? Use int.
  • Storing a price, an average, or a percentage? Use double.
  • Recording a status like "is logged in" or "is finished"? Use boolean.

If a value could ever need decimals, choose double even if your current value is whole. If a value should always be a count or whole number, choose int.

How to Use This on the AP Computer Science A Exam

Code Tracing

When you read code in the multiple-choice section, identify each variable's type right away. The declared type tells you what kind of value the variable can hold and helps you predict behavior as the code runs. Ask yourself: what type is this, and what value does it currently hold?

Problem Solving

When you write free-response code, choose the variable type before you start calculating. If a problem mentions an average or a percentage, that is a strong signal to use double. If it describes counting or indexing, int is usually right. A status or condition points to boolean.

Common Trap

Watch for type category questions. The exam may describe a scenario and ask which data type fits best. Match the description to the value: whole numbers go to int, decimals go to double, and true/false goes to boolean.

Common Misconceptions

  • "A variable's type can change later." It cannot. Once you declare a variable with a type, that type is fixed. Only the value can change while the program runs.
  • "int and double are interchangeable." They are different types with different sets of values. An int holds only whole numbers, while a double holds real numbers that can include decimals.
  • "String is a primitive type." It is not. String is a reference type. The only primitive types in this course are int, double, and boolean.
  • "A boolean can store 1 or 0." A boolean stores only true or false, not numbers.
  • "You can use a variable right after declaring it." A variable must be assigned a value before it is used in an expression. Declaring it alone is not enough.
  • "There are lots of primitive types to memorize." For this course, you only need three: int, double, and boolean. The other five are excluded from the exam.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

boolean

A primitive data type that holds one of two values: true or false.

Boolean value

Data values that can only be true or false, used in logical operations and conditional statements.

data type

A classification that specifies what kind of value a variable can store and what operations can be performed on it.

double

A primitive data type used to store real numbers (numbers with decimal points).

int

A primitive data type used to store integer values (whole numbers).

primitive data type

Basic data types in Java such as int, double, boolean, and char that are not objects.

reference type

A data type that holds a reference (memory address) to an object rather than storing the object's value directly.

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 are variables in AP Computer Science A?

A variable is a named storage location that holds a value. Every variable has a data type and a value that can change while the program runs.

What primitive data types are on the AP CSA exam?

The AP CSA exam uses three primitive data types: int for integers, double for real numbers, and boolean for true or false values.

What is the difference between primitive and reference types?

A primitive variable stores a simple value directly. A reference variable stores a reference to an object, such as a String.

When should you use int versus double?

Use int for whole-number counts or indexes. Use double for values that can include decimals, such as averages, measurements, or percentages.

What does boolean store in Java?

A boolean stores only true or false. It does not store 1 or 0 on the AP CSA exam.

Can a variable's data type change after declaration?

No. A variable's declared type stays fixed after declaration, even though the value stored in the variable can change.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot