Primitive Types, Arithmetic Expressions, Casting

In Computer Science A Unit 1, Java variables are either primitive types (int, double.

What to practice

  • Evaluate compound Java arithmetic expressions involving int, double, /, %, and compound assignment (+=, -=, *=, /=, %=).
  • Trace explicit and implicit type casting and rounding expressions (int)(x + 0.5).
  • Identify integer overflow beyond Integer.MAX_VALUE (2,147,483,647) and Integer.MIN_VALUE (-2,147,483,648).

Current CED topics: 1.2, 1.3, 1.4, 1.5, 1.6

Current learning objectives: 1.2.A, 1.2.B, 1.3.C, 1.4.A, 1.5.A, 1.6.A

1. Core Concept Instruction

In AP Computer Science A Unit 1, A variable of primitive type, such as int, double, or boolean, holds a primitive value. A variable of reference type holds a reference to an object or the null reference. This distinction describes the value a variable holds; it is not a rule that every primitive occupies stack memory. Primitive values can also be fields of objects or elements of arrays. When both operands of / are int, Java performs integer division by truncating toward zero (e.g., 17 / 5 evaluates to 3, and -17 / 5 evaluates to -3), while the modulus operator % returns the remainder (17 % 5 == 2). The unary cast operators (int) and (double) have higher precedence than *, /, and %, so (double) 17 / 5 casts 17 to 17.0 first and evaluates to 3.4, whereas (double)(17 / 5) performs integer division first and evaluates to 3.0. Rounding a positive double x to the nearest int is written (int)(x + 0.5), and rounding a negative double x is (int)(x - 0.5).

2. Worked Examples

Worked Java Expression Trace: Integer Division, Modulus & Casting Precedence

Trace the exact values of a, b, c, and d after executing:
int x = 19;
int y = 4;
double a = x / y;
double b = (double) x / y;
double c = (double) (x / y);
int d = (int) (3.7 + x % y / 2.0);
  1. Step 1 (Evaluate a = x / y): Both x (19) and y (4) are int, so 19 / 4 truncates to int 4, which widens to double a = 4.0.
  2. Step 2 (Evaluate b = (double) x / y): Unary (double) applies to x first, producing 19.0 / 4 = 4.75, so b = 4.75.
  3. Step 3 (Evaluate c = (double)(x / y)): Parentheses force integer division 19 / 4 == 4 first, which is then cast to c = 4.0.
  4. Step 4 (Evaluate d = (int)(3.7 + x % y / 2.0)): First 19 % 4 == 3; next 3 / 2.0 == 1.5; then 3.7 + 1.5 == 5.2; finally (int) 5.2 truncates to d = 5.

Result:

Compound Assignment Operators (+=, -=, *=) & 32-Bit Signed Integer Overflow

Trace the exact values of score and maxCheck after executing:
int score = 14;
score += 5 * 2;
score %= 7;
int maxCheck = Integer.MAX_VALUE + 1;
  1. Step 1 (Evaluate compound addition): In score += 5 * 2, multiplication has higher precedence than +=, so score = 14 + 10 = 24.
  2. Step 2 (Evaluate compound modulus): score %= 7 computes 24 % 7 = 3, storing 3 in score.
  3. Step 3 (Evaluate 32-bit signed overflow): Adding 1 to Integer.MAX_VALUE (2,147,483,647) wraps around to Integer.MIN_VALUE (-2,147,483,648).

Result: score = 3 and maxCheck = -2147483648 (Integer.MIN_VALUE)

Use this chapter companion

Review the lesson, attempt the original practice, then check the explanation. Your printed study guide remains complete without the website.

Independence

Skill by Skill Prep is independent. SAT® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website or book. PSAT/NMSQT® is a registered trademark of the College Board and the National Merit Scholarship Corporation, which are not affiliated with, and do not endorse, this site. Bluebook™ is a trademark of the College Board. ACT® is a trademark of ACT Education Corp., which is not affiliated with and does not endorse this website or book. Desmos is a trademark of Desmos Studio PBC. Practice results describe these questions, not official scores or admission predictions.

Last updated .