The Math Class

The operators + - * / only go so far. When you need square roots, powers, rounding, absolute values, or random numbers, Java's built-in Math class has a ready-made method waiting — no import required.

Learn The Math Class in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Math Is a Toolbox, Not an Object

The Math class is a collection of static methods. "Static" means they belong to the class itself, so you call them directly on the class name and never create an instance:

💡 Analogy: Math is like a wall-mounted toolbox in a workshop. You don't buy your own copy of the toolbox — you just walk up and grab the tool you need ( Math.sqrt , Math.max ) and put it back.

2️⃣ The Methods You'll Actually Use

Method

Does

Example → result

Math.abs(x)

Absolute value

abs(-7) → 7

Math.max(a,b)

Larger of two

max(3,9) → 9

Math.min(a,b)

Smaller of two

min(3,9) → 3

Math.pow(a,b)

a to the power b

pow(2,10) → 1024.0

Math.sqrt(x)

Square root

sqrt(144) → 12.0

Math.round(x)

Nearest whole (long)

round(4.5) → 5

Math.ceil(x)

Round up (double)

ceil(4.1) → 5.0

Math.floor(x)

Round down (double)

floor(4.9) → 4.0

Math.random()

Random 0.0–1.0

→ 0.6273...

3️⃣ Random Numbers

Math.random() returns a double in the half-open range [0.0, 1.0) — zero is possible, one is not. To turn that into a useful integer range, memorise this formula:

For a six-sided die that's min = 1, max = 6 . The (max - min + 1) gives the count of possible values, and the + min shifts the range to start at the right place.

Since the output is random, the numbers in the example below will differ every time you run it — that's the whole point!

4️⃣ Putting Math to Work

The real value of Math shows up when you combine methods into a formula. A few classics:

🧩 Reorder Challenge

Reorder these lines to compute the hypotenuse of a 3-4-5 triangle and print it as a rounded int.

Why: a and b must exist before the sqrt uses them. h (which is 5.0) must be computed before it can be rounded, and the rounded value is printed last. The output is Hypotenuse: 5 .

🧠 Quick Recall

8.0 . ceil always rounds up, and it returns a double , so note the .0 .

100 . The inner min(100, 250) is 100, then max(0, 100) is 100 — the clamp formula in action.

3. What is the type and possible range of (int)(Math.random() * 10) ?

An int from 0 to 9 inclusive. Math.random() never reaches 1.0, so * 10 never reaches 10, and the cast truncates to 0–9.

Common Beginner Mistakes

📋 Quick Reference

Returns

Math.abs(-7) → 7

Math.max(a, b)

Math.max(3, 9) → 9

Math.min(a, b)

Math.min(3, 9) → 3

Math.pow(a, b)

a to the power b (double)

Math.pow(2, 10) → 1024.0

Square root (double)

Math.sqrt(144) → 12.0

Math.cbrt(x)

Cube root (double)

Math.cbrt(27) → 3.0

Math.round(4.5) → 5

Math.ceil(4.1) → 5.0

Math.floor(4.9) → 4.0

Math.hypot(a, b)

√(a²+b²), safely

Math.hypot(3, 4) → 5.0

Random double in [0.0, 1.0)

(int)(Math.random()*(max-min+1))+min

Random int, min..max inclusive

die: min 1, max 6

Math.PI

Constant π

3.141592653589793

Math.E

Constant e

2.718281828459045

Frequently Asked Questions

🎉 Lesson Complete!

You've unlocked Java's math toolbox: abs , max / min , pow , sqrt , the rounding trio, the constants PI and E , and the random-number formula. No imports, no objects — just Math.something(...) .

Next up: StringBuilder & String Formatting — build and format text efficiently for clean, professional output.

Practice quiz

What does Math.ceil(7.01) print?

  • 7.0
  • 7
  • 8.0
  • 8

Answer: 8.0. ceil always rounds UP to the next whole number and returns a double, so 7.01 becomes 8.0.

What does Math.max(0, Math.min(100, 250)) evaluate to?

  • 100
  • 0
  • 250
  • 150

Answer: 100. The inner min(100, 250) is 100, then max(0, 100) is 100 — the standard clamp formula.

What is the type and result of Math.pow(2, 10)?

  • int 1024
  • long 1024
  • float 1024.0f
  • double 1024.0

Answer: double 1024.0. Math.pow takes two doubles and returns a double, so the result is 1024.0, not the int 1024.

Do you need to import the Math class to use Math.sqrt?

  • Yes, import java.lang.Math
  • No — it lives in java.lang
  • Yes, import java.util.Math
  • No — but you must call new Math()

Answer: No — it lives in java.lang. Math is in java.lang, which is imported automatically. There is nothing to import and nothing to instantiate.

What does Math.round(4.5) return?

  • 5
  • 4
  • 4.0
  • 5.0

Answer: 5. round returns the nearest whole number as a long; 4.5 rounds to 5.

Which call gives a random int from 1 to 6 inclusive?

  • (int)(Math.random() * 6)
  • (int)(Math.random() * 7)
  • (int)(Math.random() * 6) + 1
  • Math.random() * 6 + 1

Answer: (int)(Math.random() * 6) + 1. Math.random() is in [0.0, 1.0), so * 6 gives 0–5; adding 1 shifts it to the inclusive 1–6 range.

What does Math.floor(4.9) print?

  • 5.0
  • 4.0
  • 4
  • 5

Answer: 4.0. floor always rounds DOWN and returns a double, so 4.9 becomes 4.0.

Why does new Math() fail to compile?

  • Math is final
  • Math is an interface
  • Math is abstract
  • Math has no public constructor and only static methods

Answer: Math has no public constructor and only static methods. Every method on Math is static and it has no public constructor, so you never instantiate it — you call Math.something(...).

What does Math.sqrt(9 / 2) print?

  • 2.1213...
  • 2.0
  • 4.5
  • Compile error

Answer: 2.0. 9 / 2 is integer division giving 4, so Math.sqrt(4) is 2.0. Write 9.0 / 2 to get 4.5 first.

What is the value of Math.cbrt(27)?

  • 9.0
  • 3
  • 3.0
  • 27.0

Answer: 3.0. cbrt is the cube root and returns a double, so the cube root of 27 is 3.0.

Continue this course