impera Open Impera

AP Computer Science A · Using objects and methods

Expressions and assignment

1Arithmetic operators

Java's arithmetic operators are +, -, *, / and % (remainder).

  • Integer division: if both operands are int, the result is an int and the decimal part is truncated (cut off, not rounded). 7 / 2 is 3; -7 / 2 is -3.
  • If at least one operand is double, the result is double: 7 / 2.0 is 3.5.
  • Remainder: 17 % 5 is 2; 8 % 2 is 0. Use n % 2 == 0 to test for even numbers.
  • Dividing an int by zero throws an ArithmeticException.

Operator precedence: *, /, % before +, -; left to right for equal precedence; parentheses first.

Check yourself

What is the value of 17 / 5 * 2?

  • 6.8

  • 7

  • 6

  • 1

Show the answer

6

17 / 5 = 3, then 3 * 2.

2Assignment and compound operators

The assignment operator = evaluates the right side, then stores it in the variable on the left:

int total = 5;
total = total + 3;   // total is now 8

Compound operators combine both steps: x += 3; x -= 2; x *= 4; x /= 2; x %= 3;

Increment and decrement: x++ adds 1; x-- subtracts 1.

Worked example

trace the code.

  1. int a = 10; → a = 10.
  2. a -= 4; → a = 6.
  3. a *= 3; → a = 18.
  4. a %= 5; → a = 3.

Trace assignments one line at a time, recording each new value

Check yourself

After int n = 7; n %= 3; n++; what is n?

  • 2

  • 1

  • 3

  • 8

Show the answer

2

7 % 3 = 1, then 2.

3Casting

A cast converts a value to another type:

  • (int) 7.9 gives 7 (truncates toward zero).
  • (double) 7 gives 7.0.
  • To round a positive double to the nearest int: (int) (x + 0.5); for negative values: (int) (x - 0.5).

Java widens automatically from int to double (double d = 5; stores 5.0) but not the other way: int n = 5.0; is a compile-time error.

Worked example

average of two ints.

  1. int a = 7, b = 8;
  2. double avg = (a + b) / 2; gives 7.0 (integer division happened first).
  3. double avg = (a + b) / 2.0; gives 7.5. So does (double) (a + b) / 2.
Check yourself

What is (int) 3.99 + (int) 2.5?

  • 6.49

  • 6

  • 7

  • 5

Show the answer

5

3 + 2.

4Common pitfalls

  • Integer division in averages and percentages: count / total * 100 gives 0 if count < total. Write 100.0 * count / total.
  • Casting too late: (double) (7 / 2) is 3.0, not 3.5.
  • double arithmetic can produce tiny round-off errors: 0.1 + 0.2 is not exactly 0.3.
Check yourself

What does (double) (9 / 4) evaluate to?

  • 2.5

  • 2.25

  • 2

  • 2.0

Show the answer

2.0

Division happens first.

5Going deeper: integer division, the remainder operator, casting and tracing

Operator precedence (highest first)

  1. Parentheses.
  2. Unary minus, !, casts, ++ and --.
  3. *, /, %.
  4. +, -.
  5. Relational: less than, greater than and their or-equal forms.
  6. ==, !=.
  7. &&, then ||.
  8. Assignment (=, +=, …), which is evaluated last and from right to left.

Operators of equal precedence are evaluated left to right.

Integer division truncates toward zero

  • 7 / 2 is 3. -7 / 2 is −3. 1 / 2 is 0.
  • If either operand is a double, the division is real: 7 / 2.0 is 3.5.
  • Dividing an int by zero throws an ArithmeticException.

The remainder operator %

  • 7 % 2 is 1. 10 % 5 is 0. 3 % 7 is 3.
  • The result takes the sign of the left operand: -7 % 2 is −1.

Uses of % and /

  • Even or odd: n % 2 == 0.
  • Divisibility: n % k == 0.
  • Last digit: n % 10. Remove the last digit: n / 10.
  • Wrap around: (index + 1) % length returns to 0 after the last position.
  • Unit conversion: 137 minutes is 137 / 60 hours and 137 % 60 minutes: 2 h 17 min.

Worked example (sum of digits): trace for n = 472.

int sum = 0;
while (n > 0) {
    sum += n % 10;
    n /= 10;
}
  1. n = 472: add 2; sum = 2; n = 47.
  2. n = 47: add 7; sum = 9; n = 4.
  3. n = 4: add 4; sum = 13; n = 0.
  4. Loop ends. sum = 13.

Casting

  • (double) a / b: the cast applies to a first, so the division is real.
  • (double) (a / b): the int division happens first; the cast only adds .0.
  • With a = 7, b = 2: the first gives 3.5; the second gives 3.0.
  • (int) 3.99 is 3: truncation.

Rounding to the nearest integer

  • Positive values: (int) (x + 0.5).
  • Negative values: (int) (x - 0.5).
  • Rounding 3.5 gives (int) 4.0 = 4; rounding −3.5 gives (int) -4.0 = −4.

Compound assignment: x += 3 means x = x + 3; likewise -=, *=, /=, %=. x++ adds 1; x-- subtracts 1. (On the exam these appear as statements on their own.)

String concatenation: + with a String joins. Evaluation is still left to right.

  • "Sum: " + 2 + 3 is "Sum: 23".
  • 2 + 3 + " is the sum" is "5 is the sum".
  • "Sum: " + (2 + 3) is "Sum: 5".

Worked example (trace): what is printed?

int a = 17;
int b = 5;
double c = a / b;
a %= b;
b += a * 2;
System.out.println(a + " " + b + " " + c);
  1. a / b is int division: 3, stored as 3.0.
  2. a %= b: a = 17 % 5 = 2.
  3. b += a * 2: b = 5 + 4 = 9.
  4. Output: 2 9 3.0.

Worked example (average): scores total 257 from 3 tests.

  1. 257 / 3 gives 85: the fraction is lost.
  2. (double) 257 / 3 gives 85.666…
  3. 257 / 3.0 also works.

Common mistakes

  • Forgetting that / between two ints discards the fraction.
  • Casting the result in place of an operand.
  • Assuming % is always non-negative.
  • Expecting "Total: " + 1 + 2 to print 3.
  • Writing = (assignment) where == (comparison) is meant.
Check yourself

What is the value of (double) (9 / 2)?

  • 4.5

  • 5.0

  • 4

  • 4.0

Show the answer

4.0

The integer division happens inside the parentheses before the cast.

6For top marks

  • Trace expressions step by step, writing the type and value of each sub-expression.
  • Watch for integer division whenever two int values are divided.
  • Use % for divisibility, even/odd tests and extracting digits (n % 10 is the last digit; n / 10 removes it).
  • Remember (int) truncates; it does not round.
Example

to get the tens digit of a positive int n, use (n / 10) % 10.

Check yourself

For n = 4728, what is (n / 10) % 10?

  • 47

  • 8

  • 2

  • 7

Show the answer

2

472 % 10.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera