2s
Need help with this lesson?
๐Ÿ“šAP Computer Science Aโ€ขeasyโ€ข 9 min

Expressions, Assignment & Casting

Evaluate complex Java expressions, apply operator precedence, and correctly use casting to convert between numeric types.

An expression is anything that produces a value โ€” 3 + 4, x * 2, price / count. The rules for how Java evaluates expressions are the single richest source of multiple-choice questions in this unit.

Arithmetic operators

OperatorMeaningExampleResult
+ - *add, subtract, multiply6 * 742
/divide20 / 45
%modulo (remainder)17 % 52

The five arithmetic operators.

Integer division truncates

When both operands are int, division throws away any fractional part โ€” it does not round. 7 / 2 is 3, not 3.5 and not 4. If either operand is a double, you get true division.

System.out.println(7 / 2);     // both int  -> 3
System.out.println(7.0 / 2);   // one double -> 3.5
System.out.println(7 % 2);     // remainder  -> 1

Output:

3
3.5
1

Operator precedence

Java evaluates higher-precedence operators first. Use parentheses to force a different order.

[Diagram โ€” see the figure in the print workbook.]

Casting and type promotion

A cast (int) or (double) converts a value to another type. Casting a double to an int truncates (chops off the decimal) โ€” it never rounds. When an int and a double mix in an expression, the int is promoted to double.

[Diagram โ€” see the figure in the print workbook.]

Compound assignment and increment

x += 5 means x = x + 5. The same works for -=, *=, /=, %=. x++ adds 1; x-- subtracts 1.

Worked example 1

Integer vs. double division.

System.out.println(9 / 4);
System.out.println(9.0 / 4);
System.out.println((double) 9 / 4);

Output:

2
2.25
2.25

9 / 4 is int division โ†’ 2. Making either side a double (literally 9.0, or by casting) gives true division โ†’ 2.25.

Worked example 2

What does modulo give?

System.out.println(17 % 5);
System.out.println(20 % 4);
System.out.println(3 % 7);

Output:

2
0
3

% is the remainder after division. 17 รท 5 is 3 remainder 2. 20 divides evenly so remainder 0. When the left side is smaller, the remainder is the left side itself (3).

Worked example 3

Apply precedence.

System.out.println(3 + 4 * 2);
System.out.println((3 + 4) * 2);

Output:

11
14

Without parentheses, 4 * 2 happens first (8), then 3 + 8 = 11. Parentheses force the addition first: 7 * 2 = 14.

Worked example 4

Casting truncates.

double d = 7.9;
int n = (int) d;
System.out.println(n);

Output:

7

The cast chops the .9 away โ€” it does not round to 8. To round a non-negative number, cast (int)(d + 0.5), which here gives (int) 8.4 = 8.

Worked example 5

Compound assignment and increment.

int x = 10;
x += 5;
x *= 2;
x++;
System.out.println(x);

Output:

31

Start 10; += 5 โ†’ 15; *= 2 โ†’ 30; ++ โ†’ 31.

Common trap: The integer-division surprise

Computing an average with int values silently drops the decimal: (90 + 85) / 2 is 87, but (90 + 85) / 2.0 is 87.5. Whenever you want a decimal answer, make at least one operand a double.

Hack: Modulo is a Swiss Army knife

Modulo answers questions you will see all year: n % 2 == 0 tests even; n % 10 gives the last digit; n % k wraps a counter into the range 0..kโˆ’1 (perfect for clock arithmetic and cycling through choices).

And remember: if a decimal answer matters, one operand must be a double. When in doubt, cast.

Print companion
AP Computer Science A Power Workbook
Off-screen practice on Amazon