✕ Close

Week 15 · 15.3 · Wed, Nov 11 · The Specific Skills + Try Its

Java Math: Whole Parts, Leftovers and Decimals

This section shows the six kinds of steps on today's bench: dividing two ints, finding the remainder, keeping the fraction with a double, working in the right order, using the shortcut signs, and splitting a number into parts.

Dividing Two Ints

When both sides of / are ints, Java keeps the whole part of the answer and drops the rest. The remainder operator, written with the percent sign, gives what is left over. Together they split a division the way ÷ and rem did in the steps language.

Java      steps language      17 and 5
/         ÷                   17 / 5 is 3
%         rem                 17 % 5 is 2

How to work out a division of two ints:

  1. Find how many whole times the second number goes into the first. That is the answer to /.
  2. Multiply that back and subtract. What is left is the remainder.

Example 1. Divide 17 by 5.

1System.out.println(17 / 5);
2System.out.println(17 % 5);
Show the solution
3
2

5 goes into 17 three whole times, so 17 / 5 is 3. Three fives make 15, and 17 minus 15 leaves 2, so the remainder is 2.

Example 2. Divide a small number by a bigger one.

1System.out.println(3 % 7);
2System.out.println(7 % 7);
Show the solution
3
0

7 goes into 3 zero times, so all 3 is left over. 7 goes into 7 once, with nothing left over.

Example 3. Use the remainder to test whether a number is even.

1System.out.println(8 % 2 == 0);
2System.out.println(11 % 2 == 0);
Show the solution
true
false

An even number leaves nothing over when it is divided by 2. 8 leaves a remainder of 0, so the first line prints true. 11 leaves a remainder of 1, so the second line prints false.

Keeping the Fraction

A double is Java's type for numbers with a decimal point. A number written with a decimal point, such as 5.0, is a double. If either side of / is a double, Java divides as doubles and keeps the fraction.

Example 4. Make one side a double.

1System.out.println(17 / 5.0);
Show the solution
3.4

Example 5. Watch where the fraction is lost.

1double avg = (8 + 9) / 2;
2System.out.println(avg);
3double fixed = (8 + 9) / 2.0;
4System.out.println(fixed);
Show the solution
8.0
8.5

In line 1, 8 + 9 is 17, and 17 / 2 is worked out with two ints, so it is 8. The box is a double, so it stores 8.0, but the half was already gone. In line 3, 2.0 is a double, so 17 / 2.0 is 8.5.

Working in the Right Order

Java works multiplication, division and remainder before addition and subtraction. Operators of the same rank are worked from left to right, and parentheses are worked first.

Example 6. Multiply first, unless parentheses say otherwise.

1System.out.println(2 + 3 * 4);
2System.out.println((2 + 3) * 4);
Show the solution
14
20

In line 1, 3 * 4 is 12, and 2 + 12 is 14. In line 2, the parentheses come first: 2 + 3 is 5, and 5 times 4 is 20.

Example 7. Divide and multiply from left to right.

1System.out.println(7 / 2 * 2);
Show the solution
6

7 / 2 is worked first, and with two ints it is 3. Then 3 * 2 is 6. The half was dropped before the multiplication.

Shortcut Signs

A shortcut sign changes the value in a box. For example, c += 5 means c = c + 5. The other shortcut signs, such as *= and -=, work the same way, and c++ adds 1.

Example 8. Trace one box through four lines.

1int c = 10;
2c += 5;
3c++;
4c *= 2;
5System.out.println(c);
Show the solution
32
line   c    Output
1      10
2      15
3      16
4      32
5      32   32

Splitting a Number

Example 9. Split 125 seconds into minutes and seconds.

1int total = 125;
2int minutes = total / 60;
3int seconds = total % 60;
4System.out.println(minutes);
5System.out.println(seconds);
Show the solution
2
5

60 goes into 125 two whole times, so minutes holds 2. Two sixties make 120, so 5 seconds are left over.

The bench catches three common mistakes, and each one changes the answer. The first is keeping a fraction that integer division drops. The second is expecting a double box to bring back a half that integer division already dropped. The third is working left to right when * or / should go first.

Sources: Oracle, The Java Tutorials: Primitive Data Types, Assignment, Arithmetic, and Unary Operators and Operators; Oracle, The Java Language Specification, Java SE 21 Edition, §5.6 and §15.17; course repository, Bench Basics B17 job data (2026), run with the course JDK.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. What is printed? The first line printed is first.

1System.out.println(23 / 4);
2System.out.println(23 % 4);

Try It 2. What is printed?

1System.out.println(5 % 9);

Try It 3. What does System.out.println(9 / 2.0); print?

Try It 4. The line double half = 9 / 2; runs, then System.out.println(half); runs. What is printed?

Try It 5. What does System.out.println(10 - 4 / 2); print?

Try It 6. What does System.out.println(12 / 4 * 3); print?

Try It 7. The lines int n = 6; then n += 4; then n *= 3; run in order. What does n hold at the end?

Try It 8. What does k hold at the end?

1int k = 7;
2k++;
3k %= 5;

Try It 9. What is printed? The first line printed is first.

1int days = 17;
2System.out.println(days / 7);
3System.out.println(days % 7);

Try It 10. What is printed? The first line printed is first.

1int cm = 254;
2System.out.println(cm / 100);
3System.out.println(cm % 100);