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.

1.2.A1.2.A.11.2.A.21.2.A.31.2.B1.2.B.11.2.B.2

Reviewed against the CED on

Know this already? Jump to the checkpoint.

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 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;).

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

  1. A data type is a set of values plus the operations allowed on them.
  2. The two categories are primitive and reference.
  3. Primitive types store numbers and Boolean values; reference types are used for objects.
  4. The three primitive types in this course: int (integer), double (real number), boolean (true or false).
  5. 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.

  1. 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? 1.2.A.1, 1.2.A.2, 1.2.A.3
    Show answer

    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.

  2. 2 A variable must store the average rainfall for a week, such as 12.6. Which declaration is most appropriate? 1.2.B.1, 1.2.A.1
    Show answer

    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.

  3. 3 A game must record whether a player has finished the tutorial. Which declaration is most appropriate? 1.2.B.1
    Show answer

    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.

  4. 4 Consider the following declarations. Which variable is declared with a reference type? 1.2.A.3, 1.2.A.2
    int a = 5;
    double b = 2.5;
    boolean c = true;
    String d = "5";
    Show answer

    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.

  5. 5 Which of the following is true about the variable declared by double temp = 21.5;? 1.2.B.2
    Show answer

    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.

3PrevailLock it in for exam day

By the numbers

Name each list from memory, then reveal it to check. Each list shows how many items to recall.

  • Data type categories two categories CED 1.2.A.1
    1. primitive
    2. reference
  • Primitive types in this course (long, short, byte, float and char are outside the course) three types CED 1.2.B.1
    1. intan integer
    2. doublea real number
    3. booleantrue or false

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
  • doubletap to flip

    The primitive type for real numbers, such as 3.75 or -0.5.

    Example and CED codes
  • booleantap to flip

    The primitive type whose only values are true and false.

    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.

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure