Numbers & Arithmetic

The Course
•
4 min read

An int represents a whole number. A double represents a floating-point approximation, useful for measurements and averages. Write a decimal point when you want a double value written directly in the code: 25.0. Such a directly written value is called a literal. The values an operator works on are its operands. For division, their types determine whether the result keeps a fractional part, even if you later store the result in a different type of variable.

Java has addition, subtraction, multiplication and division: +, -, * and /. Parentheses group an expression explicitly. Multiplication and division happen before addition and subtraction unless parentheses say otherwise. A good variable name makes the units visible: minutesPerSession and sessionCount are better than two unrelated numbers in the middle of a print statement.

Division can discard information

Dividing two int values produces an int result. The fractional part is discarded toward zero. Storing that result in a double afterward cannot restore what was already lost. Use a double operand before dividing when you need a fractional result. The remainder operator % answers what is left after integer division; it is useful for complete groups and leftovers, not a percentage operator.

Choose the division before assigning Java
public class Main {
    public static void main(String[] args) {
        int whole = 7 / 2;
        double tooLate = 7 / 2;
        double fraction = 7.0 / 2;

        System.out.println(whole);
        System.out.println(tooLate);
        System.out.println(fraction);
    }
}
Check your prediction
3
3.0
3.5

Both operands in tooLate are integers, so Java first computes 3 and then stores 3.0. The decimal operand in fraction changes the division itself. Merely renaming the variable cannot change the result.

Groups and leftovers Java
public class Main {
    public static void main(String[] args) {
        int minutes = 95;
        int hours = minutes / 60;
        int leftover = minutes % 60;

        System.out.println(hours);
        System.out.println(leftover);
        System.out.println(hours * 60 + leftover);
    }
}
Check your prediction
1
35
95

One complete group of 60 and 35 leftover minutes rebuild the original 95. Test 0, 59, 60 and 120. Exactly divisible inputs have remainder zero; do not add an extra hour just because a remainder exists.

Group the intended calculation Java
public class Main {
    public static void main(String[] args) {
        System.out.println(10 + 20 / 2);
        System.out.println((10 + 20) / 2);
        System.out.println(3 * (10 + 5));
    }
}
Check your prediction
20
15
45

The first line divides 20 by 2 before adding 10. The second adds the two numbers before dividing their sum. Read the calculation in words before deciding which version matches your problem.

Your turn

Exercise

Share a study block

You have 80 minutes and want sessions of 25 minutes. Calculate and print the number of complete sessions, the leftover minutes and the decimal number of session lengths available. The results are 3, 5 and 3.2. Derive each result from the two starting values.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        int available = 80;
        int perSession = 25;
        // Calculate complete sessions, leftovers and a fractional count.
    }
}
Hint 1: the idea

Integer division counts complete groups; remainder counts what is left. The fractional calculation needs a double operand.

Hint 2: a first step

Keep perSession nonzero. Multiplying available by 1.0 produces a double before division.

Show one solution
public class Main {
    public static void main(String[] args) {
        int available = 80;
        int perSession = 25;

        System.out.println(available / perSession);
        System.out.println(available % perSession);
        System.out.println(available * 1.0 / perSession);
    }
}
3
5
3.2

Change it and explain

Try available=0, 25 and 50. Explain why 50 produces two complete sessions and zero leftover minutes. Predict before changing the values, then compare your answers to the output.

Ints have a finite range (roughly minus two billion to plus two billion), and doubles are approximations rather than exact decimal money. Keep the course’s practice values small. For exact prices in early exercises, use integer cents. Do not conclude that printing a short decimal proves every decimal calculation is exact. We do not need larger numeric types to solve today’s problems.

Was this article helpful?

Your response is saved on this device.