Lesson 3 of 34 · Fundamentals
Java Operators: Integer vs Double Math
Games count gold, banks add interest, maps measure distances — almost every program does arithmetic. Java's operators look like school math, with one twist that trips up nearly every beginner: dividing two integers gives an integer.
Arithmetic operators
int a = 17;
int b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3 ← not 3.4!
System.out.println(a % b); // 2 ← the remainder
When both sides of / are integers, Java performs integer division and simply drops everything after the decimal point (17 / 5 is 3, and -17 / 5 is -3). The modulo operator % gives the remainder that was dropped. Together they are perfect for splitting things up: 135 minutes is 135 / 60 = 2 hours and 135 % 60 = 15 minutes. n % 2 == 0 is the classic test for an even number.
Doubles and casting
If at least one side is a double, Java does decimal division:
System.out.println(17 / 5.0); // 3.4
int total = 7;
int count = 2;
System.out.println(total / count); // 3
System.out.println((double) total / count); // 3.5
System.out.println((double) (total / count)); // 3.0 ← cast too late!
System.out.println((int) 3.99); // 3 (casting to int truncates)
A cast like (double) total converts a value to another type. Java converts automatically when nothing can be lost (an int fits into a double), but you must cast explicitly in the other direction.
Keep in mind that doubles are binary fractions: 0.1 + 0.2 prints 0.30000000000000004. That is normal floating-point behaviour, not a bug in Java.
Shortcuts and precedence
int score = 10;
score += 5; // same as score = score + 5 → 15
score *= 2; // 30
score++; // add one → 31
score--; // subtract one → 30
*, / and % are evaluated before + and -, just like in math: 2 + 3 * 4 is 14. Use parentheses whenever it makes the intent clearer: (2 + 3) * 4 is 20.
The Math class
Java's built-in Math class has the usual helpers:
Math.max(3, 9) // 9
Math.min(1.5, 0.5) // 0.5
Math.abs(-7) // 7
Math.pow(2, 10) // 1024.0 (always a double)
Math.sqrt(16) // 4.0
Math.round(2.4) // 2
How the exercises work
From now on you will often fill in methods. A method is a named piece of code that receives input values and gives back a result with return:
static int square(int n) {
return n * n;
}
static int square(int n) means: a method named square that takes an int called n and returns an int. The hidden tests call your methods — for example square(4) — and compare the result with the expected value. Chapter 2 covers methods in depth; for now, just replace the placeholder return lines with real calculations.
Common mistakes
- Expecting decimals from int division:
1 / 2is0. Write1 / 2.0or cast first. - Casting too late:
(double) (a / b)converts an already-truncated result. - Integer overflow:
inttops out at 2,147,483,647.Integer.MAX_VALUE + 1silently wraps around to a negative number — uselongfor very big values.
Assignment
Implement four methods in Main:
wholeHours(int minutes)— the number of whole hours inminutes:wholeHours(135)returns2.leftoverMinutes(int minutes)— the minutes left after removing the whole hours:leftoverMinutes(135)returns15.average(int a, int b)— the exact average of two integers as adouble:average(3, 4)returns3.5.toFahrenheit(double celsius)— converts Celsius to Fahrenheit withF = C × 9 / 5 + 32:toFahrenheit(100)returns212.0.
You can print results in main to try things out; the tests call the methods directly.
Numbers and math operators in other languages
- JavaScript Numbers and Math Operators · JavaScript
- Python Numbers and Arithmetic Operators · Python
- Go Constants and Arithmetic Operators · Go
- C# Operators: Arithmetic, Comparison and Logic · C#
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .