skip to content

Math Class & StrictMath

The common Math operations and the three rounding helpers, plus StrictMath when you need bit-for-bit reproducible results across platforms. Interviewers sometimes ask what Math.round does with negative halves.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are the most common methods on java.lang.Math, and what do they compute (abs, pow, sqrt, max/min, log/exp, trig)?

level: juniorimportance: must knowfreq 55%

answer

  1. static utility class, final, no instances; PI and E constants
  2. abs / pow / sqrt / cbrt / max / min
  3. log = natural (base e), log10 = base 10, exp = e^x
  4. trig is in RADIANS — toRadians/toDegrees
  5. mostly double in, double out; abs(Integer.MIN_VALUE) overflows

basics

~10 s

Math is a utility class of static math functions: abs (magnitude), pow (x to a power), sqrt (square root), max/min (larger/smaller of two), log/exp (natural log and e^x), and sin/cos/tan (trigonometry, in radians).

solid answer

~40 s

java.lang.Math is a final utility class holding static methods and the constants PI and E; you never instantiate it. The everyday methods are abs (absolute value), pow(base, exp), sqrt, cbrt (cube root), max(a,b)/min(a,b), log (natural log, base e), log10, exp (e^x), and the trig family sin/cos/tan plus their inverses, all working in radians (use toRadians/toDegrees to convert). Most overloads take and return double. Two gotchas worth naming: trig is radians not degrees, and pow/log return double so chaining integer math through them can introduce floating-point error. For exact integer arithmetic with overflow checks Java adds addExact/multiplyExact, and Math.abs(Integer.MIN_VALUE) famously stays negative due to two's-complement overflow.

go deeper

for a junior

Can name abs, pow, sqrt, max/min and call them correctly; knows Math is static (Math.sqrt(x), no 'new').

for a middle

Knows log is natural vs log10, trig is in radians, and that results are double (approximate); can convert degrees with toRadians.

for a senior

Articulates the abs(MIN_VALUE) overflow, when to prefer addExact/multiplyExact, and why double is wrong for currency.

for a principal

Can advise team conventions: BigDecimal for money, exact-arithmetic helpers for counters, and how floating-point error propagates through chained Math calls in hot/financial paths.

## What java.lang.Math is `java.lang.Math` is a class in the Java standard library that groups together **mathematical functions** as **static methods** — methods you call on the class itself (`Math.sqrt(2)`), not on an object. The class is `final` and has a `private` constructor, so you cannot subclass it or create an instance; it is a pure namespace of functions plus two constants. - `Math.PI` — the ratio of a circle's circumference to its diameter (~3.14159), as a `double`. - `Math.E` — Euler's number, the base of the natural logarithm (~2.71828). ## The everyday methods **Absolute value — `abs`.** Returns the magnitude (distance from zero) of a number: `Math.abs(-3)` is `3`. Overloaded for `int`, `long`, `float`, `double`. **Powers and roots.** - `Math.pow(base, exponent)` raises `base` to `exponent`, e.g. `Math.pow(2, 10) == 1024.0`. Always takes and returns `double`. - `Math.sqrt(x)` is the square root (the number that, multiplied by itself, gives `x`). - `Math.cbrt(x)` is the cube root. **Comparisons — `max` / `min`.** `Math.max(a, b)` returns the larger of two values, `Math.min(a, b)` the smaller. Overloaded for the numeric primitive types. **Logarithms and exponentials.** A *logarithm* answers "to what power must the base be raised to get this number?". - `Math.log(x)` is the **natural** logarithm — base `e` (NOT base 10). - `Math.log10(x)` is base-10. - `Math.exp(x)` is `e` raised to `x`, the inverse of `Math.log`. **Trigonometry.** `Math.sin`, `Math.cos`, `Math.tan` and their inverses `asin`/`acos`/`atan` (plus `atan2(y, x)` for full-circle angles). Crucially these work in **radians**, not degrees: a full circle is `2*PI` radians, not 360. Convert with `Math.toRadians(deg)` and `Math.toDegrees(rad)`. So the sine of 90 degrees is `Math.sin(Math.toRadians(90))`, which is `1.0`. ## Types and a key gotcha Most of these methods operate on and return `double` (a 64-bit floating-point number). This means results are approximations — `Math.sqrt(2)` is not exactly the true value. The one famous trap: `Math.abs(Integer.MIN_VALUE)` returns a **negative** number, because the positive counterpart of the most-negative `int` does not fit in 32 bits (two's-complement overflow). For overflow-safe integer arithmetic Java provides `Math.addExact`, `subtractExact`, `multiplyExact`, etc., which throw `ArithmeticException` on overflow. ## When you'd reach for it Anytime you need a calculation the basic `+ - * /` operators don't give you: distances (`sqrt` of sums of squares), scaling factors (`pow`), growth/decay (`exp`/`log`), angles and geometry (trig). For exact decimal money math you would NOT use `Math`/`double` — you'd use `BigDecimal`.

  • Why does Math.abs(Integer.MIN_VALUE) return a negative number?
    Integer.MIN_VALUE is -2^31. Its positive form, 2^31, doesn't fit in a signed 32-bit int (max is 2^31-1), so two's-complement overflow wraps it back to the same negative value. abs has no positive int to return.
  • Math.log(100) doesn't give 2 — why, and what does?
    Math.log is the natural log (base e), so log(100) is ~4.605. For base 10 use Math.log10(100), which is 2.0.

saying these in an interview costs you the question

  • Thinking Math.log is base 10 (it's natural log / base e — log10 is base 10).
  • Passing degrees directly into Math.sin/cos/tan (they expect radians).
  • Believing Math.abs always returns a non-negative result (fails for Integer/Long MIN_VALUE).
  • Using Math/double for money instead of BigDecimal.

context

open as a page

Explain Math.round, Math.floor, and Math.ceil — their return types and rounding behavior, including how round handles ties and negatives.

level: middleimportance: must knowfreq 60%

basics

~20 s

floor rounds down to the nearest whole number, ceil rounds up, and round goes to the nearest whole number (rounding .5 up). floor/ceil return double; round returns long (from a double) or int (from a float).

open as a page

Why is Math (and double in general) the wrong tool for money, and what causes results like 0.1 + 0.2 != 0.3?

level: middleimportance: should knowfreq 45%

basics

~20 s

double stores numbers in binary, and many decimals like 0.1 can't be represented exactly, so tiny errors creep in (0.1 + 0.2 comes out as 0.30000000000000004). For money use BigDecimal, which stores exact decimal values.

open as a page

Math.abs and ordinary integer arithmetic can silently overflow. How does this happen, and what does Math offer to detect it?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Java ints have a fixed range, so a result too big to fit silently wraps around to a wrong (often negative) value. Math.abs(Integer.MIN_VALUE) stays negative for this reason. Math.addExact, multiplyExact, etc. throw an exception instead of wrapping.

open as a page

What is the difference between Math and StrictMath, and when would you prefer one over the other?

level: seniorimportance: should knowfreq 35%

basics

~20 s

StrictMath always gives the exact same bit-for-bit result on every platform. Math may use faster, platform-optimized routines that can differ slightly between machines. Use StrictMath when you need identical results everywhere; otherwise Math is the usual, faster choice.

open as a page