Skip to main content

1.2 Variables and Data Types

add to ChatGPT
Verified for the 2027 examUpdated October 2026Fiveable Content Team
Fiveable

💻AP Computer Science A Unit 1 Review

QR code for AP Computer Science A practice questions

1.2 Variables and Data Types

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.

A variable is a named storage location with a declared data type, and picking int when you needed double (or the reverse) can quietly change your output. Choosing the right type and declaring variables for numbers and Boolean values carry into tracing code and writing algorithms in every later unit, and multiple-choice questions ask you to spot type mismatches.

Three Primitive Types and How Variables Work

  • 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.

Tracing Code and Choosing the Right Data Type

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

  • Misconception: A variable's type can change later.
    Actually: It cannot. Once you declare a variable with a type, that type is fixed. Only the value can change while the program runs.
  • Misconception: int and double are interchangeable.
    Actually: 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.
  • Misconception: String is a primitive type.
    Actually: It is not. String is a reference type. The only primitive types in this course are int, double, and boolean.
  • Misconception: A boolean can store 1 or 0.
    Actually: A boolean stores only true or false, not numbers.
  • Misconception: You can use a variable right after declaring it.
    Actually: A variable must be assigned a value before it is used in an expression. Declaring it alone is not enough.
  • Misconception: There are lots of primitive types to memorize.
    Actually: For this course, you only need three: int, double, and boolean. The other five are excluded from the exam.

Practice Questions for 1.2

1 of 10

A fitness tracking app records a user's daily workout. It needs to store the distance run in miles (which can include fractions), the total number of steps taken, and whether the user reached their daily goal. Which of the following variable declarations uses the most appropriate data types?

A.

double distance; int steps; String goalReached;

B.

double distance; double steps; boolean goalReached;

C.

double distance; int steps; boolean goalReached;

D.

int distance; int steps; boolean goalReached;

Vocabulary

The following words are mentioned explicitly in the AP® course framework 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.

See Fiveable more often in Google

Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.

Prefer us on Google