From the free course AP Computer Science A on Lykke
1.2 Variables and Data Types
Key concepts: Java programming language · Variables · Classes · Data types · Primitive types · int values · double values · Sequential flow · Data abstractions · Compilation and code errors
A Java program becomes useful when it can remember information: a score, a temperature, a button’s state, or the name of a player. A variable is a named storage location whose value can be used while a program executes.
1.2 Variables and Data Types
A Java program becomes useful when it can remember information: a score, a temperature, a button’s state, or the name of a player. A variable is a named storage location whose value can be used while a program executes.
Essential question: How can a program represent changing real-world information, such as the buttons on a remote control?
The program’s labeled storage boxes
Imagine a row of labeled containers. One container is labeled volume and holds the integer 7; another is labeled muted and holds either true or false. The label is the variable’s name, and the kind of value the container is allowed to hold is its data type.
In Java, a variable declaration gives the data type first and the variable name second:
int volume = 7;
double screenSize = 55.5;
boolean muted = false;
An int value is an integer: a whole-number value without a fractional part, such as -3, 0, or 42. A double is a Java numeric data type used for values that may contain a decimal part, such as 55.5 or 0.25. A boolean stores one of two logical values: true or false.
| Data type | Kind of value | Example |
|---|---|---|
int |
Integer, or whole number | 7 |
double |
Number that can represent a fractional part | 55.5 |
boolean |
Logical value | false |
Primitive types and classes
These three types are primitive types, meaning Java directly supports their values as basic building blocks. A class is a programmer-defined description of objects and their behavior. Variables and classes work together: a variable can store a primitive value directly, or it can refer to an object created from a class.
For example, a remote-control program might use primitive variables for simple facts:
int channel = 12;
double batteryVoltage = 1.5;
boolean powerOn = true;
A class can organize more complex information and actions. A RemoteControl class might describe what a remote control knows—such as its current channel—and what it can do—such as changing channels. This combination of data and behavior is why variables and classes provide foundational concepts for programming solutions.
Key distinction: A variable is a named place for a value or reference. A class is a description used to create and organize objects. They are related, but they are not interchangeable.
Variables during sequential execution
Java normally executes statements in sequence, from top to bottom. When a later statement uses a variable, it observes the value most recently assigned to that variable.
int channel = 12;
channel = 4;
System.out.println(channel);
The output is:
4
The first assignment stores 12, but the second assignment replaces it with 4 before the output statement runs. This is a simple example of a data abstraction: the name channel lets the program work with the idea of a channel without needing to track the storage mechanism itself.
When the type and value disagree
Java is statically typed, so each variable has a declared type that constrains the values assigned to it. Code will not compile when an incompatible value is assigned.
int channel = 12.5; // Does not compile
double corrected = 12.5; // Correct
The value 12.5 is not an integer, so it cannot be assigned to an int variable. Correct the error by choosing double, or by using a genuinely whole-number value:
int channel = 12;
Named misconception — “int means any number.” It does not. int represents whole numbers; double represents numeric values that may include fractional parts. A decimal value is not automatically converted to an int merely because the variable was declared as one.
AP skill connection
This topic develops Computational Thinking Practice 1—Design Code: choosing variables and data types that represent the problem accurately. It also develops Computational Thinking Practice 2—Develop Code: writing declarations, assignments, and corrections that compile and behave as intended.
Retrieval check: What is printed?
int volume = 3;
volume = 8;
System.out.println(volume);
The answer is 8, because sequential execution reaches the second assignment before the output statement. Which declaration would correctly store 2.75: int measurement or double measurement? The correct choice is double measurement.


Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A
- abstractions.