Computer Science A · Unit 1: Primitive Types · 16 min read · Updated 2026-05-11
Primitive Types — AP Computer Science A
AP Computer Science A · Unit 1: Primitive Types · 16 min read
1. Core Tested Primitive Types★☆☆☆☆⏱ 3 min
AP CS A only tests three of the eight total Java primitive types: `int`, `double`, and `boolean`. Each has a specific use case, storage size, and valid value range.
2. Type Casting and Operator Precedence★★☆☆☆⏱ 4 min
Implicit (widening) casting happens automatically when converting from a smaller/less precise type to a larger/more precise type, with no risk of data loss. For AP CS A, the only tested widening conversion is `int` → `double`, and no extra syntax is required.
Explicit (narrowing) casting is required when converting from a larger type to a smaller type, which may cause data loss. When casting a `double` to `int`, Java truncates the decimal component (no rounding occurs). Syntax is `(targetType) value` placed before the value to cast.
Java evaluates expressions following a fixed order of operator precedence, from highest to lowest priority:
Parentheses `()`
Explicit cast `(type)`
Unary operators: `+`, `-`, `!`
Multiplicative operators: `*`, `/`, `%`
Additive operators: `+`, `-`
Relational operators: `<`, `>`, `<=`, `>=`
Equality operators: `==`, `!=`
Logical AND `&&`
Logical OR `||`
Assignment operators: `=`, `+=`, etc.
3. Integer Division and Remainder Operator★★☆☆☆⏱ 3 min
When both operands of the division operator `/` are `int`, Java performs integer division: it truncates the result towards zero and returns an `int` with no decimal component. This is one of the most frequently tested traps on the AP CS A exam.
The remainder operator `%` returns the value left over after integer division of the first operand (dividend) by the second. The sign of the result always matches the sign of the dividend.
$7 / 2 = 3$ (not 3.5, both operands are `int`)
$-7 / 2 = -3$ (truncated towards zero, not -4)
$7 / 2.0 = 3.5$ (one operand is `double`, so floating-point division occurs)
$7 \% 2 = 1$, $-7 \% 2 = -1$, $7 \% -2 = 1$
4. Assignment and Compound Assignment Operators★★★☆☆⏱ 4 min
The basic assignment operator `=` evaluates the entire right-hand side expression first, then stores the result in the variable on the left-hand side. A critical common mistake is confusing `=` (assignment) with `==` (equality comparison) in conditionals.
Compound assignment operators combine a mathematical operation and assignment into a single statement, with syntax `x op= y`. This is equivalent to `x = (type of x) (x op y)`, with an implicit cast to the type of the left-hand variable, which allows narrowing conversions without an explicit cast.
`+=`: Add and assign: `x += 4` ≡ `x = x + 4`
`-=`: Subtract and assign: `x -= 2` ≡ `x = x - 2`
`*=`: Multiply and assign: `x *= 3` ≡ `x = x * 3`
`/=`: Divide and assign: uses integer division if `x` is `int`
`%=`: Remainder and assign: `x %= 5` ≡ `x = x % 5`
5. Practice Concept Check★★★☆☆⏱ 2 min
Common Pitfalls
Why: Students forget that `/` between two `int`s truncates the decimal component, resulting in an incorrect integer value before assignment to `double`.
Why: Students assume left-to-right evaluation, so only the first value is cast before multiplication.
Why: Similar syntax and confusion from mathematical notation where `=` means equality.
Why: Experience with other languages leads students to try casting between `int` and `boolean`.
Why: Students forget that compound assignment adds an implicit cast to the type of `x`, leading to unexpected results or compile error assumptions.