TOPIC 1.2 · STUDY

Variables and Data Types

A data type names a set of values and the operations on them. This topic uses the primitives int, double, and boolean, and contrasts them with a reference type for an object. Declaring a variable writes the type, then the name, then an optional initial value.

1.2.A

Choosing a data type

A data type is a set of values together with the operations allowed on those values. When you store a quantity in a program, you pick a type that matches what the quantity can be. The wrong type either cannot hold the value or invites the wrong operations.

AP CSA teaches three primitive types in this topic. int holds whole numbers. double holds numbers that may include a fractional part. boolean holds only true or false. Each of these stores a single primitive value directly in the variable.

Type Kind of value Example value
int Whole number only 17
double Number that may include a fraction 2.75
boolean True or false only true
Three side-by-side boxes labeled int, double, and boolean, each with a one-word example value under the type name.

Pick the box that matches the kind of value you need to store, not a nearby numeric stand-in.

A reference type is a different choice. An object that bundles several fields—say a TrailStop with a name, a mile marker, and notes—is a reference type. A variable of that type stores a reference to the object, not a packed int or a Boolean. One primitive field inside the object does not make the object type itself primitive.

Match the specification, not a workaround. A whole-number count is int, not boolean. A reading such as 9.8 is double, not int. A yes-or-no flag is boolean, not int coded as 1 and 0. A single count or flag is not automatically an object type.

Key points

  • A data type is a set of values plus the operations on them.
  • int is for whole numbers; double is for values that may include a fraction; boolean is for true or false.
  • A primitive variable holds the primitive value itself.
  • A reference type variable holds a reference to an object, not a single packed primitive.

1.2.B

Declaring a variable

A variable is a name tied to a memory location that can hold a value of a chosen type. To declare one in Java, write the type keyword first, then the identifier (the variable name), then a semicolon. You may give an optional initial value after an equals sign before the semicolon.

int trailMarkers;
double gradePercent = 6.5;
boolean gateOpen;

The first and third lines declare without an initial value. The middle line declares and initializes in one statement. In every case the type keyword comes before the name. The course integer keyword is int, not the word integer. Reversing the order, or omitting the type, is not a valid declaration.

The type keyword decides what the variable holds. The wording of the identifier does not. Writing int gateReady; still creates an integer variable, even if the name sounds like a yes-or-no flag. A Boolean variable must use the keyword boolean.

Key points

  • Declaration order is type, then name, then an optional initial value, then a semicolon.
  • Use the keywords int, double, and boolean—not look-alike English words.
  • An initial value is allowed but not required in this topic.
  • The type keyword, not the identifier spelling, determines the type.

Do not let a suggestive name override the declared type. int gateReady; holds an integer. To store true or false, declare boolean gateReady;. Type comes first in the line, and type—not the identifier—sets what values are allowed.

Practice this topic