Skip to content
Numbers and Math
step 1/5

Reading — step 1 of 5

Learn

~3 min readGetting Started

Numbers and Math

Java's arithmetic operators look familiar — + - * / % — but / hides the trap that bites every beginner exactly once: when both operands are integers, division is integer division. The fractional part is not rounded. It is thrown away.

int a = 7, b = 3;
System.out.println(a / b);   // 2  — not 2.333
System.out.println(a % b);   // 1  — the remainder
System.out.println(a * b);   // 21

Why would a language do this? Type predictability. In Java, the type of an expression is determined by the types of its operands — int / int is always int. The compiler never silently promotes your result into a floating-point number you didn't ask for. You opt in to decimals explicitly.

Getting real division

To get 2.333..., at least one operand must be a double. The tool is a cast:

int a = 7, b = 3;
System.out.println((double) a / b);     // 2.3333333333333335

(double) a converts a before the division happens, so the whole expression becomes floating-point. Here's the near-miss version that still fails:

System.out.println((double) (a / b));   // 2.0 — too late!

The parentheses force a / b to run first as integer division (result 2), and only then cast — you converted the already-truncated answer. Cast an operand, not the finished result.

The remainder operator %

a % b gives what's left over after integer division. It powers the most common tests in programming:

n % 2 == 0    // n is even
n % 2 != 0    // n is odd
n % 5 == 0    // n is divisible by 5

You'll use exactly this in the Conditionals lesson for FizzBuzz.

The Math class

For anything beyond the basic operators, java.lang.Math (auto-imported) has static methods:

Math.max(3, 9);     // 9
Math.min(3, 9);     // 3
Math.abs(-5);       // 5
Math.sqrt(144);     // 12.0 — returns double
Math.pow(2, 10);    // 1024.0 — returns double
Math.PI;            // 3.141592653589793

Trap: Math.pow and Math.sqrt always return double. System.out.println(Math.pow(2, 3)) prints 8.0, not 8 — an exact-match grader will fail that. For small integer powers, just multiply: n * n.

Know your limits

int is 32 bits — roughly plus/minus 2.1 billion. Exceed that and the value wraps around silently: no error, just a wrong number. When quantities can get big — file sizes, timestamps, world populations — use long, and remember the L suffix on literals: long big = 10_000_000_000L;.

Your exercise

Read a width and a height (the starter's Scanner already does it) and print the rectangle's area:

int w = sc.nextInt();
int h = sc.nextInt();
System.out.println(w * h);

For input 5 and 3 the grader expects exactly 15. The mistake it will catch: doing the math in floating point — (double) w * h prints 15.0 and fails. These are ints, the product is an int, print the bare int. No labels either — Area: 15 fails; print just the number.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…