Java Math Methods

Beginner
7 min read
Updated: Nov 2026
57 method guides
Method index

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.

Math works on primitives.

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:

Java
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 methods

Drop the minus sign, pick the larger or smaller value, keep a value inside a range, or read and copy the sign of a number.

MethodDescriptionTutorial
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 methods

Turn a decimal number into a whole number: always up, always down, to the nearest even neighbour, or the school-book way.

MethodDescriptionTutorial
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 methods

Plain int and long operators wrap around silently when the result is too big. These methods throw ArithmeticException instead.

MethodDescriptionTutorial
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 methods

Integer division that rounds down or up instead of toward zero, the matching remainders, and the IEEE remainder for doubles.

MethodDescriptionTutorial
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 methods

Squares and powers, square and cube roots, distances, e to the x, and natural or base-10 logarithms.

MethodDescriptionTutorial
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 methods

Sine, cosine, and tangent, their inverses and hyperbolic versions, and conversion between degrees and radians.

MethodDescriptionTutorial
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 method

A quick pseudo-random double for dice, games, and sampling.

MethodDescriptionTutorial
random()A pseudo-random double from 0.0 (inclusive) up to 1.0 (exclusive).Try it

Floating-point internals

6 methods

Look inside a float or double: its binary exponent, the gap to the next value, the neighbouring values, and fast scaling by powers of two.

MethodDescriptionTutorial
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

They are the static methods of java.lang.Math, a built-in class for everyday number work: absolute values, rounding, powers and roots, logarithms, trigonometry, random numbers, and integer arithmetic that detects overflow.
This index lists 57 guides, from abs() and absExact() to toRadians() and ulp(), grouped by what they do.
Neither. Math lives in java.lang, which every Java file sees automatically, and its constructor is private. Call the methods on the class name, for example Math.sqrt(16.0).
fma, multiplyFull, and multiplyHigh need Java 9; absExact needs Java 15; ceilDiv, ceilMod, ceilDivExact, floorDivExact, and divideExact need Java 18; clamp needs Java 21. Everything else works on Java 8.
Read the Java Math hub first. Then open abs(), round(), pow(), sqrt(), max(), and floorMod() from the tables below.

Start with abs()

Drop the minus sign from any number, and learn the one int value it cannot handle.

abs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful