W3docs

Java Math Class

Perform common mathematical operations in Java with Math.abs, Math.sqrt, Math.pow, Math.random, and other static methods.

The java.lang.Math class is a collection of static methods for the math operations that go beyond +, -, *, /, %. Absolute values, powers, roots, trig, logarithms, rounding, random numbers — they all live here. The class is implicitly imported, so you can call its methods directly: Math.sqrt(2).

Absolute value, min, max

Math.abs(-7);          // 7
Math.abs(-3.14);       // 3.14

Math.min(3, 5);        // 3
Math.max(3, 5);        // 5
Math.min(1.5, 1.7);    // 1.5

All four are overloaded for int, long, float, and double.

Powers and roots

Math.pow(2, 10);       // 1024.0  — always returns double
Math.sqrt(2);          // 1.4142135623730951
Math.cbrt(27);         // 3.0
Math.exp(1);           // 2.718281828... — e^x

For integer exponents, Math.pow is overkill — a loop or << is faster.

Logarithms

Math.log(Math.E);      // 1.0  — natural log (ln)
Math.log10(1000);      // 3.0  — base-10 log
Math.log(8) / Math.log(2);   // 3.0 — log base 2

(Java 11+ also has Math.log and Math.exp accurate variants — for most code these are fine.)

Rounding

MethodBehavior
Math.floor(x)round down (toward -∞), returns double
Math.ceil(x)round up (toward +∞), returns double
Math.round(x)round to nearest, ties round up; returns long for double, int for float
Math.rint(x)round to nearest, ties round to even; returns double
Math.floor(2.7);       // 2.0
Math.ceil(2.1);        // 3.0
Math.round(2.5);       // 3   — ties round up
Math.round(-2.5);      // -2  — toward positive infinity
Math.rint(0.5);        // 0.0 — banker's rounding
Math.rint(1.5);        // 2.0

For decimal-aware rounding (e.g., to two decimal places for money), use BigDecimal with a RoundingMode.

Trigonometry

All trig functions work in radians. Convert with Math.toRadians / Math.toDegrees:

Math.sin(Math.PI / 2);          // 1.0
Math.cos(0);                    // 1.0
Math.tan(Math.PI / 4);          // 0.999999... (≈ 1)

Math.toRadians(180);            // Math.PI
Math.toDegrees(Math.PI);        // 180.0

Math.atan2(1, 1);               // π/4  — handles quadrant correctly

Math.atan2(y, x) is the right tool for "what's the angle of this vector," not Math.atan(y/x).

Constants

Math.PI;       // 3.141592653589793
Math.E;        // 2.718281828459045

Random numbers

Math.random() returns a double uniformly in [0.0, 1.0):

double r = Math.random();          // 0.0 ≤ r < 1.0
int dieRoll = 1 + (int)(Math.random() * 6);  // 1..6

For anything more (seeded, repeatable, ranges, gaussians), use java.util.Random or java.util.concurrent.ThreadLocalRandom:

import java.util.Random;
Random rng = new Random(42);   // seeded, reproducible
int n = rng.nextInt(100);      // 0..99
double g = rng.nextGaussian(); // normal distribution

For security tokens, don't use Math.random or Random — use java.security.SecureRandom.

Overflow-checked arithmetic

Math.addExact, subtractExact, multiplyExact, negateExact, incrementExact, decrementExact throw ArithmeticException on integer overflow:

Math.addExact(Integer.MAX_VALUE, 1);  // throws ArithmeticException

Useful when correctness matters more than speed.

A demonstration

java— editable, runs on the server

What's next

Java User Input with Scanner — reading numbers and text from the terminal.

Practice

Practice

What does Math.pow(2, 10) return?