Skip to lesson

Arithmetic operators

An operator is a symbol that does a job with values. The arithmetic operators +, -, *, / and % do maths: add, subtract, multiply, divide and find the remainder.

The arithmetic operators

At the canteen you buy 2 samosas at Rs 15 and 3 teas at Rs 12, and want the bill and your change from Rs 100.

The values an operator works on are its operands: in samosa * 2, they are samosa and 2.

Example 1 · Canteen bill

Canteen.javaJava
public class Canteen {    public static void main(String[] args) {        int samosa = 15;        int tea = 12;        int total = samosa * 2 + tea * 3;        System.out.println("Total: Rs " + total);        int paid = 100;        int change = paid - total;        System.out.println("Change: Rs " + change);    }}
Output
Total: Rs 66
Change: Rs 34

What this code does

  • samosa * 2 and tea * 3 run first, then + adds them: 30 + 36 = 66.
  • int total = ... stores the answer so the next lines can use it. - then finds the change: 100 - 66 = 34.
  • The other operators work the same way: 10 - 3 is 7 and 10 / 2 is 5. / with whole numbers has a surprise, shown next.
*, / and % run before + and -, like in school maths. Operator precedence and parentheses (opens in a new tab) covers the full order.

Division: int or double?

Three friends split a bill of Rs 1000. You expect Rs 333.33 each, but Java prints 333.

Integer division: when both numbers are int, the answer is an int and the decimal part is dropped.

Example 2 · Split a bill among friends

SplitBill.javaJava
public class SplitBill {    public static void main(String[] args) {        int bill = 1000;        int friends = 3;        System.out.println(bill / friends);             // 333, not 333.33        System.out.println((double) bill / friends);        System.out.println((double) (bill / friends));    }}
Output
333
333.3333333333333
333.0

What this code does

  • bill / friends divides two int values, so the answer is the whole number 333.
  • (double) bill turns 1000 into 1000.0 before dividing, so the decimals stay.
  • The last line casts too late: bill / friends is already 333, so the cast only adds .0.

Java drops the decimals; it does not round, so 7 / 2 is 3, not 4. Make either side a double and the decimals stay: 7.0 / 2 gives 3.5. With two int variables, cast one of them before dividing, as in Type conversion and casting (opens in a new tab).

The remainder operator %

You pack 17 sweets into boxes of 5. / tells you there are 3 full boxes, but how many sweets are left over?

The remainder operator % gives what is left after dividing: 17 % 5 is 2. People read it as "17 mod 5".

17 sweets packed in boxes of 5 fill 3 boxes (15 sweets) and leave 2 sweets over. So 17 / 5 gives 3 and 17 % 5 gives 2.

Example 3 · Uses of %

Remainder.javaJava
public class Remainder {    public static void main(String[] args) {        System.out.println(27 % 2);        System.out.println(28 % 2);        System.out.println(4729 % 10);        int minutes = 135;        System.out.println(minutes / 60 + " hours");        System.out.println(minutes % 60 + " minutes");    }}
Output
1
0
9
2 hours
15 minutes

What this code does

  • 27 is odd, so 27 % 2 gives 1. 28 is even, so 28 % 2 gives 0.
  • 4729 % 10 gives the last digit, 9.
  • / gives the full hours and % gives the minutes left over.
Java's % keeps the sign of the left number: -27 % 10 is -7, not 3 or 7. So the even/odd and last-digit tricks are for numbers that are 0 or more.

Dividing by zero

An app shows the average marks of a class with total / students. On the first day nobody has joined, so students is 0.

Java
int total = 0;int students = 0;int average = total / students;   // ArithmeticException: / by zero

An exception is an error that happens while the program runs: the code compiles, but the program stops at that line. % by zero stops the same way.

With a double, Java does not stop: 10.0 / 0 gives Infinity, which is just as wrong for an average.

Try it below. Change a and b, try a negative a, then set b to 0.

Experiment

Division lab

Change a and b. Watch / and % change, then set b to 0.

5
int a = 17;
int b = 5;
System.out.println(a / b);
System.out.println(a % b);
System.out.println((double) a / b);
a / b
3
a % b
2
(double) a / b
3.4
Check the divisor before dividing: students != 0. Logical operators (opens in a new tab) shows the safe way to write it.

Next step

Check your understanding

10 quick questions