What are the most common methods on java.lang.Math, and what do they compute (abs, pow, sqrt, max/min, log/exp, trig)?
answer
- static utility class, final, no instances; PI and E constants
- abs / pow / sqrt / cbrt / max / min
- log = natural (base e), log10 = base 10, exp = e^x
- trig is in RADIANS — toRadians/toDegrees
- mostly double in, double out; abs(Integer.MIN_VALUE) overflows
basics
~10 sMath 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 sjava.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
Can name abs, pow, sqrt, max/min and call them correctly; knows Math is static (Math.sqrt(x), no 'new').
Knows log is natural vs log10, trig is in radians, and that results are double (approximate); can convert degrees with toRadians.
Articulates the abs(MIN_VALUE) overflow, when to prefer addExact/multiplyExact, and why double is wrong for currency.
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.