Unit 1 · 12 min
1.2 Variables and Data Types
Every value has a type, and the type decides what values are allowed and what you can do with them.
Summary, objectives and course and exam description (CED) codes
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.
By the end of this session you can
- 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.
Reviewed against the CED on
1LearnThe notes, the traps and how to remember them
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 asPlant.
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 asintornew, and Java is case-sensitive (scoreandScoreare different variables). Declaring creates a variable (int x;); initializing gives it its first value (x = 5;).
Lock it in: memory hooks
- Primitive vs referenceA primitive variable holds the value itself; a reference variable holds directions to an object.
- The three primitivesCount, measure, decide: int counts, double measures, boolean decides.
Common misconceptions
"String is a primitive type because it is used so often."
Not quite. String is a class, so it is a reference type. The only primitive types in this course are int, double and boolean.
"A double is just a bigger int."
Not quite. 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.
"A boolean can hold yes, no, 0 or 1."
Not quite. A boolean holds only true or false.
Exam traps
- Choose the type from the specification. A count of people is an int, a price or an average is a double, a yes/no state is a boolean, text is a String (a reference type).
- String starts with a capital letter because it is a class: a reference type, not a primitive.
2PracticeRecall it, then check yourself
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.
Cover this list, say the points out loud, then take the checkpoint without scrolling back up.
Checkpoint · 5 questions
Checkpoint: Variables and Data Types
Pick an answer to mark it. Every answer is explained, and you can try any question again. Your saved score counts first tries only, and stays in this browser.
3PrevailLock it in for exam day
By the numbers
Key terms: flip the cards
Read the term, say the definition out loud, then flip to check. Struggling a little to remember is what makes it stick.
-
Data typetap to flip
A set of values and a corresponding set of operations on those values. Every data type is primitive or reference.
Example and CED codes -
Primitive typetap to flip
A data type for numbers and Boolean values whose variables hold the value directly. In this course: int, double and boolean.
Example and CED codes -
Reference typetap to flip
A data type used to define objects, that is, anything that is not a primitive type. Every class defines a reference type.
Example and CED codes -
inttap to flip
The primitive type for integers, such as -4, 0 or 120. An int is stored in 4 bytes.
Example and CED codes -
Variabletap to flip
A named storage location that holds a value, which can change while the program runs. Every variable has a name and a data type.
Example and CED codes -
nulltap to flip
A special literal meaning a reference variable is not associated with any object.
Example and CED codes -
Object referencetap to flip
The value a reference variable holds: it can be thought of as the memory address of the object.
Example and CED codes
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.