AP COMPUTER SCIENCE A | UNIT 1: PRIMITIVE TYPES
1.2 Variables and Data Types
Master the building blocks of Java programming. These study notes cover variable declaration and initialization, primitive data types (int, double, boolean), reference types, identifying valid variable names, and potential pitfalls in data storage for the AP CSA exam.
Big Idea: Storage and Types
A variable is a named storage location in the computer's memory. In Java, every variable has a type (what kind of data it holds) and a name (how we refer to it). Java is a strongly typed language, meaning you must declare the type of a variable before you can use it, and that type cannot change.
Declaring and Initializing Variables
To use a variable, you must first create it. This process involves two steps: declaration (creating the box) and initialization (putting a value in the box).
Syntax
1. Declaration:
int score;
Creates a variable named score that can hold integers.
2. Initialization:
score = 0;
Assigns the value 0 to the variable score.
Combined Step
You can (and usually should) do both in one line:
double price = 19.99;
boolean isOver = false;
The single equals sign = is the
assignment operator. It assigns the value on the right to the variable on
the left.
Primitive Data Types
Java has 8 primitive
data types, but AP Computer Science A strictly tests only three: int, double, and
boolean. (You rarely see char, but it's good to know).
| Type | Description | Size | Examples |
|---|---|---|---|
| int | Integer (whole numbers) | 32 bits | 42, -7, 0, 2025 |
| double | Decimal numbers (floating point) | 64 bits | 3.14, -0.01, 5.0 |
| boolean | True or false values | 1 bit* | true, false |
| char | Single character (in single quotes) | 16 bits | 'A', '?', '9' |
int (Integer)
Stores whole numbers from approx. \(-2\) billion to \(2\) billion.
Min: \(-2^{31}\) Max: \(2^{31}-1\)
AP Alert: If you exceed the max value, an integer overflow occurs, wrapping around to the minimum negative value!
double (Decimal)
Stores numbers with fractional parts. Even if the number is
whole (e.g., 5), logic dictates storing it as 5.0 to be a double.
Used for measurements like height, weight,
temperature, and money (though big financial apps use BigDecimal).
boolean (Logic)
Stores only two possible values: true or
false.
Used for flags (`isGameOver`), loop conditions, and if-statements.
Reference Data Types (Strings)
A reference
type stores the memory address (reference) of an object, not the data itself. The most common
reference type in Unit 1 is String.
String vs Primitive Types
- Primitives (int, double, boolean): Start with a lowercase letter. Hold the actual value.
- Reference (String, Scanner): Start with an Uppercase Letter (they are classes). Hold a reference to an object.
String greeting = "Hello World";
Naming Variables
Java has strict rules and conventions for naming identifiers (variables, classes, methods).
The Rules (Must Follow)
- Can contain letters, numbers,
_, and$. - Cannot start with a number.
- Cannot be a Java keyword (e.g.,
class,int,void). - Case-sensitive:
scoreandScoreare different.
The Conventions (Should Follow)
- Variables: use
camelCase(start lowercase, new words capitalized). Example:highScore,isRaining. - Classes: use
PascalCase(start uppercase). Example:Student,Car. - Constants: use
ALL_CAPS(with underscores). Example:MAX_LEVEL.
Constants (final Variables)
If you use the
final keyword, the variable becomes a constant. Its value cannot be changed
once assigned. Attempting to change it causes a compiler error.
// PI = 3.15; <-- ERROR: cannot assign a value to final variable PI
Common Mistakes
- Uninitialized Variables: Declaring a variable without assigning a value leads to errors if you try to use it. Local variables in methods do not get default values automatically.
- Type Mismatch:
Trying to store a
doubleinto anintwithout casting is a compiler error (loss of precision). - Integer Division:
Dividing two integers results in an integer (truncating the decimal).
5 / 2is2, not2.5. - Confusing
=and==: The single equals=is for assignment. The double equals==is for comparison (checking equality). - Overflow: Adding 1 to the maximum integer value wraps around to the minimum negative value.
Official and Recommended Resources
The following are verified official and authoritative resources for AP Computer Science A.
College Board — AP CSA Course
The official AP Computer Science A course description, exam details, and sample questions.
apcentral.collegeboard.orgOracle Java Documentation
The official documentation for the Java programming language from Oracle. The ultimate source of truth.
docs.oracle.comKhan Academy — AP CSA
Free video lessons and practice exercises specifically aligned with the AP CSA curriculum.
khanacademy.orgCSAwesome
An interactive, free textbook for AP CSA that runs Java code directly in the browser.
csawesome.runestone.academyTest Your Knowledge: Variables & Types Quiz
Check your understanding of variable declaration, initialization, and data types.
Key Takeaways for the AP Exam
- Strong Typing: You must declare the variable type (`int`, `double`, `boolean`) before using it.
- Assignment: The
single equals
=assigns the value on the right to the variable on the left. - Primitives vs References: Primitives hold values; references (`String`) hold memory addresses.
- Integer Division:
Be careful!
int / intcreates anintby truncating the decimal part. - Overflow:
Integer.MAX_VALUE + 1results inInteger.MIN_VALUE. - Constants: Use the
finalkeyword for variables that should not change.
Frequently Asked Questions
What is the difference between `int` and `double`?
`int` stores whole numbers (integers) like 5, -10, or 0. `double` stores numbers with decimal points (floating-point numbers) like 3.14, -0.001, or 5.0. Integers are exact; doubles are approximations used for continuous values like height or weight.
Can I store a decimal value in an `int` variable?
No, you cannot directly store a `double` in an `int`. Java prevents this because it would lose data (the decimal part). You must explicitly cast it to an int (e.g., `(int) 3.9`), which truncates the decimal part (resulting in `3`).
What happens if I don't initialize a variable?
For local variables (variables inside a method), Java will give a compiler error if you try to use them before initialization. Instance variables (fields in a class) are automatically initialized to default values (0 for int, 0.0 for double, false for boolean, null for objects).
Why is `String` capitalized but `int` is not?
`int` is a primitive type built into the language. `String` is a class (a reference type). By convention in Java, class names start with an uppercase letter, while primitive types and variable names start with a lowercase letter.
What are valid variable names?
Assignments must start with a letter, underscore `_`, or dollar sign `$`, and can contain numbers. They cannot start with a number. They cannot be Java keywords (like `class` or `while`). Case matters (`total` vs `Total`).
What is an overflow error?
An overflow error happens when a calculation produces a result that exceeds the storage capacity of the data type. for an `int`, identifying a value larger than \(2^{31}-1\) causes it to wrap around to negative numbers.
About the Author
Adam
Co-Founder @ RevisionTown
Expert in Computer Science education with a focus on AP CSA and introductory programming. Passionate about making complex coding concepts accessible through clear visualisations and practical examples.
Connect on LinkedInRelated on RevisionTown
Start here (prerequisites)
- 1.1 Why Programming? Why Java?
- How to Code: Comprehensive Guide
- AP Computer Science A: Primitive Types Hub
Practice and application
- How to Learn Python (Compare with Java)
- Variables and Expressions (Math Context)
- Data Mathematics Resources

