| Study Guides
College Board · cb-cs-a · AP Computer Science A · Primitive Types · 16 min read · Updated 2026-05-07

Primitive Types — AP Computer Science A CS A Study Guide

For: AP Computer Science A candidates sitting AP Computer Science A.

Covers: Core primitive data types (int, double, boolean), type casting and operator precedence, integer division and remainders, mathematical and assignment operators, and compound assignment rules for Java.

You should already know: Basic Java or any other procedural-language programming.

A note on the practice questions: All worked questions in the "Practice Questions" section below are original problems written by us in the AP Computer Science A style for educational use. They are not reproductions of past College Board papers and may differ in wording, numerical values, or context. Use them to practise the technique; cross-check with official College Board mark schemes for grading conventions.


1. What Is Primitive Types?

Primitive types are the most basic, built-in data types in Java that store raw, non-object values directly in system memory, rather than referencing a memory address like reference types (e.g. String, ArrayList). There are 8 total primitive types in Java, but the AP CS A syllabus only tests 3 high-utility types: int, double, and boolean. These types form the foundation of all variable declarations, calculations, and conditional logic in Java, and make up the entire content of Unit 1 in the official AP CS A Course and Exam Description (CED). Unlike reference types, primitive types do not have methods, and their values are copied directly when assigned to new variables.

2. int, double, boolean

Each of the 3 testable primitive types has a fixed storage size, value range, and use case:

int

A 32-bit signed integer that stores whole numbers (positive, negative, or zero) with no decimal component. Its valid range is to , or -2147483648 to 2147483647. Use int for counts, indices, whole-number measurements, and any value that will never require decimal precision. Worked example:

int studentCount = 142; // Valid: whole number
int invalidValue = 3000000000; // Compile error: exceeds int range

double

A 64-bit floating-point number that stores decimal values and numbers in scientific notation, with a range of roughly to . Use double for averages, scientific measurements, and calculations requiring decimal precision. Note that doubles have minor floating-point precision errors (e.g. ), so you should never compare doubles for exact equality with == on the exam. Worked example:

double testAverage = 87.63;
double gravity = 9.81;

boolean

A 1-bit type that only holds two possible values: true and false. Unlike languages like C++, Java booleans have no numeric equivalent, and you cannot cast between int and boolean. Use booleans for conditional checks, loop conditions, and flag variables. Worked example:

boolean isPassing = testAverage >= 70.0;
boolean isWeekend = false;

AP CS A does not test other primitive types (char, long, float, byte, short), so you do not need to memorize their ranges or use cases for the exam.

3. Type casting and operator precedence

Type casting

Type casting is the process of converting a value from one primitive type to another, and falls into two categories:

  1. Implicit (widening) casting: Automatic conversion from a smaller, less precise type to a larger, more precise type with no risk of data loss. The valid widening order for tested types is intdouble. No explicit syntax is required. Example: int age = 17; double ageDouble = age;ageDouble holds 17.0
  2. Explicit (narrowing) casting: Manual conversion from a larger type to a smaller type, which may cause data loss. You must add the cast operator (targetType) before the value to cast. When casting a double to int, Java truncates the decimal component (no rounding). Example: double price = 19.99; int priceDollars = (int) price;priceDollars holds 19 A common exam trap is casting order: the cast operator has very high precedence, so (int) 8.99 * 2 evaluates to , while (int) (8.99 * 2) evaluates to .

Operator precedence

Java evaluates expressions in a fixed order of operator priority, from highest to lowest:

  1. Parentheses ()
  2. Explicit cast (type)
  3. Unary operators: +, -, !
  4. Multiplicative operators: *, /, %
  5. Additive operators: +, -
  6. Relational operators: <, >, <=, >=
  7. Equality operators: ==, !=
  8. Logical AND: &&
  9. Logical OR: ||
  10. Assignment operators: =, +=, -= etc. Worked example: Evaluate Step 1: Cast 4.5 to int → 4 Step 2: Evaluate Step 3: Evaluate Step 4: Evaluate Exam tip: When in doubt, add extra parentheses to make your intended order explicit; this is never penalized and eliminates calculation errors.

4. Integer division and remainders

Integer division

When both operands of the division operator / are int, Java performs integer division, which 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. Examples:

  • (not 3.5)
  • (not -4)
  • (one operand is double, so standard floating-point division runs)

Remainder operator %

The remainder operator returns the value left over after integer division of the first operand by the second. The sign of the result always matches the sign of the first operand (dividend). Examples:

  • Common use cases for % on the exam include checking if a number is even/odd ( = even), extracting digits from a number (), and clock arithmetic ( for 2 PM). Worked example: A teacher has 127 pencils to distribute equally to 21 students. How many pencils does each student get, and how many are left over? Solution: Each student gets pencils, and pencil is left over. Verify: .

5. Mathematical and assignment operators

Mathematical operators

The tested mathematical operators for primitive types are:

  • +: Addition (also used for String concatenation, a reference type operation)
  • -: Subtraction
  • *: Multiplication
  • /: Division (integer or floating-point depending on operands)
  • %: Remainder
  • Unary -: Negates a numeric value
  • !: Logical NOT, flips a boolean value

Assignment operator =

The assignment operator evaluates the entire right-hand side of the expression first, then stores the resulting value in the variable on the left-hand side. The left-hand side must always be a variable, not a literal or fixed value. Critical distinction: The single = is an assignment operator, while the double == is an equality check. Writing if (x = 5) instead of if (x == 5) is one of the most common syntax errors tested on the exam; for int variables, this will throw a compile error, while for boolean variables it will incorrectly assign the value and run the conditional. Worked example: Evaluate the following code:

int a = 10;
int b = 3;
double c = 4.0;
boolean d = a / b > c;

Step 1: a / b is integer division: Step 2: Compare → false Result: d holds false

6. Compound assignment

Compound assignment operators combine a mathematical operation and assignment into a single statement, with the 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 that allows narrowing conversions without an explicit cast. The tested compound operators are:

  • +=: Add and assign: x += 4x = x + 4
  • -=: Subtract and assign: x -= 2x = x - 2
  • *=: Multiply and assign: x *= 3x = x * 3
  • /=: Divide and assign: x /= 2 (uses integer division if x is int)
  • %=: Remainder and assign: x %= 5x = x % 5 Key exam note: The implicit cast means compound assignment will not throw a compile error for narrowing conversions, unlike standard assignment. For example:
int x = 5;
x += 3.5; // Valid: equivalent to x = (int)(5 + 3.5) = 8
x = x + 3.5; // Compile error: assigning double to int without explicit cast

Worked example: What is the value of sum after the following code runs?

int sum = 0;
sum += 10;
sum += 15;
sum /= 5;

Step 1: sum starts at 0, add 10 → 10 Step 2: Add 15 → 25 Step 3: Integer division by 5 → Result: sum holds 5

7. Common Pitfalls (and how to avoid them)

  • Pitfall: Using integer division when you need a decimal result. Why? Students forget that / between two ints truncates decimals. Correct: Cast one operand to double first, e.g. double avg = (double) total / count instead of total / count.
  • Pitfall: Forgetting the cast operator has higher precedence than multiplicative operators. Why? Students assume left-to-right evaluation. Correct: Wrap calculations in parentheses before casting if you want to convert the final result, e.g. (int)(a * b) instead of (int)a * b.
  • Pitfall: Confusing assignment = with equality == in conditionals. Why? Similar syntax, habit from mathematical notation. Correct: Double-check all conditional statements, or use "Yoda conditions" (e.g. if (5 == x)) which throw a compile error if you accidentally write =.
  • Pitfall: Assuming boolean values are equivalent to 1 and 0. Why? Experience with languages like C++. Correct: Java booleans only hold true/false; you cannot cast between int and boolean, and if (x) is only valid if x is a boolean.
  • Pitfall: Ignoring implicit casting in compound assignment. Why? Students assume x += y works exactly like x = x + y. Correct: Remember compound assignment adds an implicit cast to the type of x, so narrowing conversions are allowed without explicit cast.

8. Practice Questions (AP Computer Science A Style)

Question 1

What is the output of the following code snippet?

int a = 14;
int b = 4;
double c = 3.0;
boolean result = (a / b) * 2 > (a / c) + 1;
System.out.println(result);

Solution

Step 1: Evaluate a / b (integer division): Step 2: Multiply by 2: Step 3: Evaluate a / c (floating-point division, a is implicitly cast to double): Step 4: Add 1: Step 5: Compare true Final output: true

Question 2

What is the value of z after the following code executes?

int z = 5;
z += 2.5;
z *= 3;
z %= 4;

A) 1 B) 2 C) 3 D) 21 E) Compile error

Solution

Correct answer: A) 1 Step 1: z += 2.5 is equivalent to z = (int)(5 + 2.5) = (int)7.5 = 7 Step 2: z *= 3 Step 3: z %=4 Option E is incorrect because compound assignment includes an implicit cast, so no compile error occurs.

Question 3

A bakery has 97 cookies to pack into boxes that hold 12 cookies each. Any leftover cookies are given to staff. Write a single line of Java code to calculate the number of leftover cookies, stored in an int variable called leftover.

Solution

int leftover = 97 % 12;

Explanation: The remainder operator returns the number of cookies left after packing as many full 12-cookie boxes as possible. , so leftover cookie.

9. Quick Reference Cheatsheet

Concept Rule
int 32-bit signed integer, range to , stores whole numbers
double 64-bit floating point, stores decimal values, has minor precision errors
boolean Only values true and false, no numeric equivalent in Java
Implicit Casting Automatic widening: intdouble, no data loss
Explicit Casting Syntax: (targetType) value, used for narrowing, truncates decimals when casting double to int
Operator Precedence (highest to lowest) () → cast (type) → unary +/-/!* / %+ - → relational → equality → && → `
Integer Division When both operands of / are int, result is int, truncated towards zero
Remainder % Returns remainder after integer division, sign matches dividend
Compound Assignment x op= yx = (type of x) (x op y), implicit cast allows narrowing conversions

10. What's Next

Primitive types are the foundational building block of all Java programming, and you will use them constantly across every other unit in the AP CS A syllabus. Integer division and remainder operators are essential for writing loops, array index manipulation, and algorithm problems like digit extraction or modular arithmetic that appear frequently in the Free Response Question (FRQ) section. Boolean values are the core of conditional logic (if/else statements, while loops, for loops) that you will learn in Unit 3, and type casting rules will come up again when you work with numeric calculations in methods, inheritance, and array operations later in the course.

Mastering these rules now will save you countless hours debugging syntax and logic errors as you progress to more complex topics. If you have any questions about primitive type behavior, operator rules, or exam-specific tricks for this unit, you can ask Ollie, our AI tutor, for personalized explanations and extra practice problems at any time on the homepage.

← Back to topic

Stuck on a specific question?
Snap a photo or paste your problem — Ollie (our AI tutor) walks through it step-by-step with diagrams.
Try Ollie free →