Java Math Methods

About This Index
This page is the Java Math Methods index: every static method of the java.lang.Math class, each with a full guide covering syntax, all overloads, parameters, return value, special cases such as NaN and overflow, three runnable examples, and FAQs. Every example is a complete Java class with fixed input values, so the output shown is the output you get.
Learn what the Math class is on the Math hub. Use this page when you need a specific method such as round(), pow(), or floorMod(). Examples compile on Java 8 and later, except for methods that are themselves newer; their pages say which version they need.
Most methods take and return double. A few, such as abs, max, min, and the *Exact family, also have int, long, or float overloads. Java picks the overload from the argument types.
How to Call a Math Method
Every method here is static and java.lang needs no import: write the class name, a dot, and the method:
double root = Math.sqrt(16.0); // 4.0
long rounded = Math.round(2.5); // 3
int bigger = Math.max(7, 12); // 12
int wrapped = Math.floorMod(-1, 7); // 6
double area = Math.PI * Math.pow(2, 2); // 12.566370614359172 Java Math Methods Table
All method guides in this Java tutorial series (57 tutorials), grouped by purpose.
Absolute value, min/max, clamp, and sign
7 methodsDrop the minus sign, pick the larger or smaller value, keep a value inside a range, or read and copy the sign of a number.
| Method | Description | Tutorial |
|---|---|---|
abs() | Get the absolute (non-negative) value of an int, long, float, or double. | Try it |
absExact() | Absolute value that throws ArithmeticException instead of returning a negative MIN_VALUE (Java 15+). | Try it |
max() | The larger of two int, long, float, or double values. | Try it |
min() | The smaller of two int, long, float, or double values. | Try it |
clamp() | Keep a value inside a min..max range with one call (Java 21+). | Try it |
signum() | The sign of a number as -1.0, 0.0, or 1.0. | Try it |
copySign() | Return the first number with the sign of the second number. | Try it |
Rounding
4 methodsTurn a decimal number into a whole number: always up, always down, to the nearest even neighbour, or the school-book way.
| Method | Description | Tutorial |
|---|---|---|
ceil() | Round a double up to the next whole number (the result is still a double). | Try it |
floor() | Round a double down to the previous whole number (the result is still a double). | Try it |
rint() | Round a double to the nearest whole number; halves go to the even neighbour. | Try it |
round() | Round to the nearest int or long; halves round up, toward positive infinity. | Try it |
Exact (overflow-safe) integer arithmetic
12 methodsPlain int and long operators wrap around silently when the result is too big. These methods throw ArithmeticException instead.
| Method | Description | Tutorial |
|---|---|---|
addExact() | Add two ints or longs and throw ArithmeticException on overflow. | Try it |
subtractExact() | Subtract two ints or longs and throw ArithmeticException on overflow. | Try it |
multiplyExact() | Multiply two ints or longs and throw ArithmeticException on overflow. | Try it |
multiplyFull() | Multiply two ints and get the exact result as a long (Java 9+). | Try it |
multiplyHigh() | The upper 64 bits of the 128-bit product of two longs (Java 9+). | Try it |
negateExact() | Change the sign of an int or long and throw ArithmeticException on overflow. | Try it |
incrementExact() | Add 1 to an int or long and throw ArithmeticException on overflow. | Try it |
decrementExact() | Subtract 1 from an int or long and throw ArithmeticException on overflow. | Try it |
toIntExact() | Convert a long to an int and throw ArithmeticException if it does not fit. | Try it |
divideExact() | Integer division that throws on the MIN_VALUE / -1 overflow (Java 18+). | Try it |
ceilDivExact() | Like ceilDiv, but throws ArithmeticException on overflow (Java 18+). | Try it |
floorDivExact() | Like floorDiv, but throws ArithmeticException on overflow (Java 18+). | Try it |
Division and remainder
5 methodsInteger division that rounds down or up instead of toward zero, the matching remainders, and the IEEE remainder for doubles.
| Method | Description | Tutorial |
|---|---|---|
ceilDiv() | Integer division that rounds the quotient up, toward positive infinity (Java 18+). | Try it |
ceilMod() | The remainder that goes with ceilDiv. Its sign is opposite to the divisor (Java 18+). | Try it |
floorDiv() | Integer division that rounds the quotient down, toward negative infinity. | Try it |
floorMod() | Remainder whose sign follows the divisor. Ideal for wrapping indexes and clocks. | Try it |
IEEEremainder() | IEEE 754 remainder: x minus the nearest whole multiple of y. | Try it |
Powers, roots, and logarithms
10 methodsSquares and powers, square and cube roots, distances, e to the x, and natural or base-10 logarithms.
| Method | Description | Tutorial |
|---|---|---|
pow() | Raise a number to a power: a to the b. | Try it |
sqrt() | Square root of a double. Negative input gives NaN. | Try it |
cbrt() | Cube root of a double. Works for negative numbers too. | Try it |
hypot() | The length sqrt(x*x + y*y) without overflow, for example a distance between points. | Try it |
exp() | Euler's number e raised to a power. | Try it |
expm1() | Compute e to the x, minus 1, accurately for x close to zero. | Try it |
log() | Natural logarithm (base e) of a double. | Try it |
log10() | Base-10 logarithm of a double. | Try it |
log1p() | Compute ln(1 + x) accurately for x close to zero. | Try it |
fma() | Fused multiply-add: a * b + c with a single rounding step (Java 9+). | Try it |
Trigonometry and angles
12 methodsSine, cosine, and tangent, their inverses and hyperbolic versions, and conversion between degrees and radians.
| Method | Description | Tutorial |
|---|---|---|
sin() | Sine of an angle given in radians. | Try it |
cos() | Cosine of an angle given in radians. | Try it |
tan() | Tangent of an angle given in radians. | Try it |
asin() | Arc sine: the angle in radians (-pi/2 to pi/2) whose sine is the given value. | Try it |
acos() | Arc cosine: the angle in radians (0 to pi) whose cosine is the given value. | Try it |
atan() | Arc tangent: the angle in radians (-pi/2 to pi/2) whose tangent is the given value. | Try it |
atan2() | The angle of the point (x, y) in radians, from -pi to pi, in the correct quadrant. | Try it |
sinh() | Hyperbolic sine of a double. | Try it |
cosh() | Hyperbolic cosine of a double. | Try it |
tanh() | Hyperbolic tangent of a double, always between -1 and 1. | Try it |
toDegrees() | Convert an angle from radians to degrees. | Try it |
toRadians() | Convert an angle from degrees to radians. | Try it |
Random numbers
1 methodA quick pseudo-random double for dice, games, and sampling.
Floating-point internals
6 methodsLook inside a float or double: its binary exponent, the gap to the next value, the neighbouring values, and fast scaling by powers of two.
| Method | Description | Tutorial |
|---|---|---|
getExponent() | The unbiased binary exponent of a float or double. | Try it |
nextAfter() | The neighbouring float or double in the direction of a second value. | Try it |
nextDown() | The next smaller float or double value. | Try it |
nextUp() | The next larger float or double value. | Try it |
scalb() | Multiply a float or double by a power of two: d times 2 to the n. | Try it |
ulp() | The size of one unit in the last place: the gap to the next representable number. | Try it |
Frequently Asked Questions
Start with abs()
Drop the minus sign from any number, and learn the one int value it cannot handle.
12 people found this page helpful
