Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Numbers and Math
Java's operators + - * / % look familiar, but two of them behave in ways that surprise people coming from Python or JavaScript. By the end of this lesson you can split a quantity into whole units and a remainder, get real (decimal) division when you need it, and avoid the classic wrong answers.
Integer division discards the fraction
When both operands are integers, / is integer division: the fractional part is thrown away, not rounded.
int a = 7, b = 3;
System.out.println(a / b); // 2
System.out.println(a % b); // 1
System.out.println(10 / 4 * 4); // 8, not 10: (10 / 4) is 2 first
% is the remainder: what is left after the division. Together, / and % split a quantity into whole units and leftovers. With 100 eggs sold in boxes of 12:
int eggs = 100;
int boxes = eggs / 12; // 8 full boxes
int loose = eggs % 12; // 4 left over
The two always agree: boxes * 12 + loose == eggs. When a value has to be split across several units (days, hours, minutes), take / for the big unit, then apply % to carry the remainder into the next step. Each step works on the remainder of the one before.
Operators evaluate left to right, by precedence
*, / and % bind tighter than + and -; equal-precedence operators go left to right. Use parentheses to say what you mean: a + b / 2 is not (a + b) / 2.
Getting real division
At least one operand must be a double for decimal division:
System.out.println((double) a / b); // 2.3333333333333335
System.out.println((double) (a / b)); // 2.0, too late
In the first line a becomes a double before the division. In the second, a / b already ran as integer division and produced 2; casting that gives 2.0. Cast an operand, not the finished result. A decimal literal also works: a / 2.0.
Negative numbers
Integer division truncates toward zero, and % takes the sign of the left operand:
System.out.println(-7 / 2); // -3
System.out.println(-7 % 3); // -1
So n % 2 == 1 is false for -3 (its remainder is -1). To test for oddness safely, compare against zero: n % 2 != 0. If you need a result that is never negative, Math.floorMod(-7, 3) gives 2.
The Math class
java.lang.Math holds static helpers:
Math.max(3, 9); // 9
Math.abs(-5); // 5
Math.sqrt(144); // 12.0, a double
Math.pow(2, 10); // 1024.0, a double
sqrt and pow always return double, so println(Math.pow(2, 3)) prints 8.0, not 8. For small integer powers, multiply: n * n.
Shortcuts
x += 5 means x = x + 5, and x++ means x = x + 1. Both also exist for -, *, /, %.
Common mistakes
- Expecting a decimal from
int / int.1 / 2is0, not0.5. - Casting after dividing.
(double) (a / b)has already lost the fraction. - Testing oddness with
== 1. It misses negative odd numbers. - Forgetting that
%carries, so computing every unit from the original total instead of from the remainder of the previous step double-counts.
Your exercise
Convert a duration given in seconds into days, hours, minutes and seconds. Work from the largest unit down and let each step consume what it can. The tests include exact multiples (where a unit's remainder is zero) and values near the limit of int.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…