Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Java Basics  ›  Capstone Project

Math Operations and Type Casting

Math Operations 14 minArithmetic between two ints produces an int, Java widens smaller numeric types automatically, and narrowing to a smaller type needs an explicit cast.

Math Operations and Type Casting

Brew Bar splits a bill of 7 coffees between 2 friends and the till says each owes 3. Nobody pressed a wrong key: Java did exactly what the types asked for. Arithmetic in Java depends on the types of the numbers involved, and casting is how you change a type on purpose.

The rule

Int with int gives int; widen freely, narrow only with a cast.

The operators are the usual ones: +, -, *, /, and % for the remainder. The surprise is division. When both sides are whole numbers, / throws away the fraction:

int cups = 7;
System.out.println(cups / 2);    // 3, not 3.5
System.out.println(cups % 2);    // 1, the remainder
System.out.println(cups / 2.0);  // 3.5, one side is a double

If either side is a double, Java does the calculation in double. That is widening: Java converts a smaller type into a bigger one automatically, along byte → short → int → long → float → double, because nothing important is lost.

Going the other way is narrowing, and Java makes you ask for it with a cast, a type in brackets:

double average = 3.99;
int whole = (int) average;             // 3: the fraction is cut off, not rounded
long nearest = Math.round(average);    // 4: rounding is a separate method

Two more things behave differently from ordinary maths:

  • The cast applies to what comes right after it. (double) 7 / 2 casts the 7 first, giving 3.5. (double) (7 / 2) divides first, giving 3, then widens to 3.0.
  • Whole numbers overflow silently. Integer.MAX_VALUE + 1 wraps around to a large negative number instead of failing. Use long when totals could get big.

The Math class supplies the rest: Math.max(a, b), Math.min, Math.abs, Math.pow, Math.sqrt.

The shortcut

Before reading any result, write the type above each number, then apply the rule step by step. An int / int step always loses its fraction, even if the answer is stored in a double afterwards.

Average price of an orderStep 1 of 4
  1. Label the types

    int totalCents = 950; and int drinks = 4; are both int.

Drills

Answer fast — trust the rule.

quick drill

What does System.out.println(17 % 5); print?

quick drill

int priceCents = 250; double dollars = priceCents / 100; What is dollars?

quick drill

What is (int) 9.99?

quick drill

Which expression evaluates to 3.5?

quick drill

Which assignment needs an explicit cast to compile?

Remember

  • Int with int gives int; widen freely, narrow only with a cast.
  • Write the type over each number and work step by step.
  • Traps: (int) cuts rather than rounds, a cast binds to the very next value, and ints overflow silently.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe bill that split wrong

A bill of 1,450 cents is split between 4 friends with double share = billCents / friends; where both are ints. Say what share ends up as, explain why, and write a corrected line. Then say what (int) 362.5 would give and why.