# AP® Computer Science A 1.2 Variables and Data Types

Source: https://lockincs.com/ap-csa/unit-1/1-2-variables-and-data-types/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.2.A, 1.2.A.1, 1.2.A.2, 1.2.A.3, 1.2.B, 1.2.B.1, 1.2.B.2

A data type is a set of values and the operations allowed on them, and every data type is either primitive or reference. The three primitive types in this course are int, double and boolean. A variable is a named storage location with a data type, and its value can change while the program runs.

## What you will learn

- 1.2.A Identify the most appropriate data type category for a particular specification.
- 1.2.B Develop code to declare variables to store numbers and Boolean values.

## 1\. What a data type is

A **data type** is a set of values and the set of operations allowed on those values. Every data type is in one of 2 categories: **primitive** or **reference** 1.2.A.1.

-   **Primitive types** define the values and operations for numbers and Boolean values 1.2.A.2. The variable stores the value directly.
-   A **reference type** is used for objects, that is, for anything that is not a primitive type 1.2.A.3. Examples: `String`, or a class such as `Plant`.

The type decides which operations make sense. You can multiply two `int` values, but you cannot multiply two `boolean` values.

## 2\. The three primitive types in this course

The three primitive types used in this course are `int`, `double` and `boolean` 1.2.B.1.

| Type | Holds | Example values |
| --- | --- | --- |
| `int` | an integer | `-4`, `0`, `120` |
| `double` | a real number | `3.75`, `-0.5`, `87.5` |
| `boolean` | only `true` or `false` | `true`, `false` |

Outside the course: Java has 5 other primitive types (`long`, `short`, `byte`, `float` and `char`). You will not need them on the AP® exam.

## 3\. Declaring variables

A **variable** is a storage location that holds a value, and the value can change while the program is running. Every variable has a name and a data type. A variable of a primitive type holds a primitive value of that type 1.2.B.2.

```
int lives = 3;            // whole number
double average = 87.5;    // real number
boolean gameOver = false; // true or false only
String player = "Ana";    // reference type: an object
System.out.println(player + " has " + lives + " lives");
System.out.println("Average: " + average);
System.out.println("Game over? " + gameOver);
lives = 2;                // the value can change
System.out.println(player + " has " + lives + " lives");
```

Output:

```
Ana has 3 lives
Average: 87.5
Game over? false
Ana has 2 lives
```

The name and type stay the same for the life of the variable. Only the value changes.

## 4\. Primitive and reference variables in memory

A primitive variable holds the value itself. A reference variable holds a reference (an address) that leads to an object somewhere else in memory.

| Code | What the variable holds |
| --- | --- |
| `int count = 3;` | `count` holds the value `3`. |
| `boolean done = false;` | `done` holds the value `false`. |
| `Plant p = new Plant("Fern", 30);` | `p` holds a reference to a `Plant` object, and that object stores name `"Fern"` and height `30`. |
| `Plant q = null;` | `q` holds `null`: there is no object. Calling a method on `q` throws a `NullPointerException` (topic 1.14). |

Assigning `p` to another variable copies the reference, not the object, so both variables then refer to the same object (topics 1.4, 1.12 and 1.14).

```
Plant p = new Plant("Fern", 30);
Plant other = p;           // copies the reference
other.grow(5);
System.out.println(p.getHeight());
```

Output:

```
35
```

There is only one `Plant` object. Growing it through `other` changes what `p` sees too.

## 5\. Choosing the right type for a specification

Read the specification and ask: what values must this hold? 1.2.A

| Specification | Best type | Category |
| --- | --- | --- |
| Number of people on a bus | `int` | primitive |
| Price of a ticket, average score | `double` | primitive |
| Whether a door is locked | `boolean` | primitive |
| A player’s name | `String` | reference |

**Watch out:** do not store a number you will calculate with as text. `"12.6"` is a `String`, so you cannot do arithmetic with it.

> **Background (not tested): naming conventions.** Java programmers name variables in camelCase (`totalScore`, `isReady`) and classes in PascalCase (`Plant`, `String`). Names cannot start with a digit, contain spaces, or be a keyword such as `int` or `new`, and Java is case-sensitive (`score` and `Score` are different variables). Declaring creates a variable (`int x;`); initializing gives it its first value (`x = 5;`).

## Common mix-ups

- Not true: String is a primitive type because it is used so often. String is a class, so it is a reference type. The only primitive types in this course are int, double and boolean.
- Not true: A double is just a bigger int. An int holds an integer, such as -4 or 120. A double holds a real number, such as 3.75. Use the type that matches the values you need.
- Not true: A boolean can hold yes, no, 0 or 1. A boolean holds only true or false.

## In 30 seconds

- A data type is a set of values plus the operations allowed on them.
- The two categories are primitive and reference.
- Primitive types store numbers and Boolean values; reference types are used for objects.
- The three primitive types in this course: int (integer), double (real number), boolean (true or false).
- A variable is a named storage location with a data type; its value can change while the program runs.

## Checkpoint: 5 practice questions

### Question 1

A program stores a club member's name and the number of meetings the member attended. Which best describes the data type category of each value?

- A. Both values are primitive.
- B. Both values are reference types.
- C. The name is a reference type; the number of meetings is primitive.
- D. The name is primitive; the number of meetings is a reference type.

**Answer: C.** A name is a `String`, a reference type; a count of meetings is an `int`, a primitive. A is wrong because `String` is not a primitive type. B is wrong because a whole-number count fits the primitive type `int`. D swaps the two categories.

### Question 2

A variable must store the average rainfall for a week, such as 12.6. Which declaration is most appropriate?

- A. `int rain = (int) 12.6;`
- B. `String rain = "12.6";`
- C. `double rain = 12.6;`
- D. `int rain = 13;`

**Answer: C.** 12.6 is a real number, so `double`. A and D lose the .6: A truncates to 12 and D rounds by hand to 13. B stores text, which cannot be used in arithmetic.

### Question 3

A game must record whether a player has finished the tutorial. Which declaration is most appropriate?

- A. `boolean finished = false;`
- B. `String finished = "false";`
- C. `double finished = 0.0;`
- D. `int finished = 0;`

**Answer: A.** A yes/no state is a `boolean`, which holds only `true` or `false`. B stores the text "false", which is a `String`, not a Boolean value. C and D use number types to fake a yes/no value, which allows meaningless values such as 0.5 or 7.

### Question 4

Consider the following declarations. Which variable is declared with a reference type?

```java
int a = 5;
double b = 2.5;
boolean c = true;
String d = "5";
```

- A. `a`
- B. `b`
- C. `c`
- D. `d`

**Answer: D.** `String` is a class, so `d` has a reference type and holds a reference to a `String` object. `a`, `b` and `c` use the three primitive types `int`, `double` and `boolean`, which store numbers and Boolean values directly.

### Question 5

Which of the following is true about the variable declared by `double temp = 21.5;`?

- A. Its value can change, but its type stays double.
- B. Its value is fixed for as long as the program runs.
- C. It can later be assigned the String "warm".
- D. It holds a reference to an object that stores 21.5.

**Answer: A.** A variable has a name and a data type, and its value can change while the program runs. B is wrong because a variable's value can change. C is wrong because a `double` variable can hold only `double` values. D is wrong because `double` is a primitive type, so the variable holds the value itself.

## Questions students ask

**What are the primitive data types in AP® Computer Science A?**

Only three: int for integers, double for real numbers and boolean for true or false. The other five Java primitives (long, short, byte, float and char) are outside the course.

**Is String a primitive type?**

No. String is a class, so String is a reference type. A String variable holds a reference to a String object.

**What is the difference between a primitive type and a reference type?**

A primitive variable stores its value directly, such as the number 3. A reference variable stores a reference to an object, which you can think of as the object's memory address.

**When should I use int and when double?**

Use int for whole-number quantities, such as a count of people or a number of lives. Use double when the value can have a decimal part, such as a price, an average or a temperature like 21.5.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
