AP Computer Science A Unit 1: Primitive Types
Study variables, data types, expressions, assignment, casting with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
Primitive types are the basic building blocks of Java programs. This unit covers int, double, and boolean data types, arithmetic operators, and how Java evaluates expressions including integer division and casting.
Why it matters
Every Java program uses primitive types. Understanding integer division, type casting, and operator precedence prevents subtle bugs that the AP exam specifically tests. These concepts appear throughout all subsequent units.
Key concepts
- Java has eight primitive types; the AP subset focuses on int, double, and boolean.
- Integer division truncates: 7/2 evaluates to 3, not 3.5.
- Casting converts between types: (double) 7/2 evaluates to 3.5.
- The modulus operator % returns the remainder and is useful for checking divisibility.
Variables and Data Types
A variable is a named storage location with a specific type. Declaring a variable specifies its type and name: int count = 0; or double price = 9.99;. Java is strongly typed, meaning you cannot assign a String to an int variable. The int type stores whole numbers, double stores decimal numbers, and boolean stores true or false. Variable names should be descriptive and follow camelCase convention. Constants use the final keyword and ALL_CAPS naming.
Arithmetic Expressions
Java follows standard operator precedence: parentheses first, then multiplication/division/modulus, then addition/subtraction. When both operands of division are int, Java performs integer division and truncates the decimal portion. The expression 7/2 gives 3, not 3.5. To get decimal division, at least one operand must be a double: 7.0/2 gives 3.5. The modulus operator % returns the remainder: 7%2 gives 1. These behaviors are heavily tested on the AP exam.
Type Casting and Compound Assignment
Casting explicitly converts a value from one type to another. Casting (int) 3.7 truncates to 3 (it does not round). Casting (double) 5 produces 5.0. Java automatically widens int to double in mixed expressions, but narrowing (double to int) requires an explicit cast. Compound assignment operators like +=, -=, *=, and /= combine an operation with assignment. The increment (++) and decrement (--) operators are shorthand for adding or subtracting 1.
AP exam tip
On the AP exam, trace through arithmetic expressions step by step, paying close attention to integer division. The expression (int)(5.0/2) gives 2, while (int)5.0/2 also gives 2, but 5/2.0 gives 2.5.
Connections to other units
- Unit 2: Objects wrap primitive types and add functionality — Integer and Double are wrappers for int and double.
- Unit 3: Boolean expressions are built from primitive comparisons and drive all control flow.
- Unit 4: Loop counters are typically int variables that use arithmetic operators.