Variables &
Data Types
Learn how Java stores information using variables, and master the primitive data types that form the foundation of every AP Computer Science A program.
What is a Variable?
A variable is a named storage location in memory that holds a value. Think of it as a labeled box: the label is the variable's name, and the contents are its current value.
Declaring and Initializing
Declaration creates the variable. Initialization gives it a first value. You can do both in one statement or separately.
Why Data Types Matter
Java is a statically typed language: the compiler checks types before your code runs. Choosing the right type affects memory usage, precision, and allowed operations.
Java's Primitive Data Types
Java has 8 primitive types. The AP CSA exam focuses on three: int, double, and boolean.
The int Type: Whole Numbers
An int stores a 32-bit signed integer. It can hold values from \( -2^{31} \) to \( 2^{31} - 1 \), which is roughly -2.1 billion to +2.1 billion.
The double Type: Decimals
A double stores a 64-bit floating-point number. Use it whenever you need decimal precision: measurements, averages, scientific calculations.
The boolean Type: True or False
A boolean can only hold two values: true or false. Booleans drive conditions, loops, and logical expressions.
Primitive vs. Reference Types
Java has two categories of types. Primitive types store the value directly. Reference types store a pointer to an object in memory.
Two collaborators work on one shared model
Java assignment copies a value. For primitive variables this is the primitive value; for object variables it is a reference to an object.Assignment copies a value
After x is changed to 9, y keeps its copied value of 42. For String variables, assignment copies a reference: reassigning a to "world" leaves b referring to the unchanged "hello" object.Naming Variables Correctly
Choosing the Right Type
Example 1: Basic Declarations
A student tracker program needs to store a student's name, age, GPA, and whether they are enrolled.
Example 2: Integer Division Trap
What does Java compute for each expression?
Example 3: Type Casting
Widening (int to double) happens automatically. Narrowing (double to int) requires an explicit cast and truncates the decimal.
Common Mistakes to Avoid
Summary Comparison
| Feature | int | double | boolean |
|---|---|---|---|
| Stores | Whole numbers | Decimal numbers | true / false |
| Size | 32 bits (4 bytes) | 64 bits (8 bytes) | 1 bit (JVM varies) |
| Default Value (fields) | 0 | 0.0 | false |
| Example | int n = 10; | double d = 2.5; | boolean b = true; |
| Division | Truncates (\(7/2 = 3\)) | Preserves (\(7.0/2 = 3.5\)) | N/A |
Question 1 of 5
What is the value of result after this code runs?int result = 13 / 5;
Question 2 of 5
Which of the following is a valid Java variable name?
Question 3 of 5
What is the output of the following code?double x = 7 / 2;
System.out.println(x);
Question 4 of 5
What is the value of y after this code executes?double pi = 3.99;
int y = (int) pi;
Question 5 of 5
Which data type should you use to store whether a user has accepted the terms of service?