In Java, java.lang.Math is a built-in class of static methods for number work: rounding, powers and roots, logarithms, trigonometry, random numbers, and integer arithmetic that detects overflow. The class is final with a private constructor and lives in java.lang, so you never import it and never write new Math(). You just call Math.sqrt(2.0).
It also has two constants: Math.PI (3.141592653589793) and Math.E (2.718281828459045), the base of natural logarithms. Popular methods include abs(), round(), pow(), sqrt(), max(), and random(). The Math Methods index lists every guide.
Math vs StrictMath.
StrictMath has the same methods but always uses one fixed set of algorithms, so results are identical on every machine. Math may use faster processor-specific code for sin, exp, pow, and similar functions, so their last digit can differ between platforms. abs, sqrt, floor, and round are exact either way.
Syntax
Java Syntax
Write the class name, a dot, and the method. No import needed:
Most methods work only on double. A few have one version per type, picked from your arguments:
Methods
Overloads
Example
abs, max, min
int, long, float, double
Math.max(3, 9) is 9; Math.max(3, 9.0) is 9.0
round
float to int, double to long
Math.round(2.5f) is an int
addExact and other *Exact
int, long
Math.addExact(5L, 7L) is the long 12
sqrt, pow, sin, ...
double only
Math.sqrt(9) returns 3.0
Concept
Special Values: NaN, Infinity, and -0.0
Double math never throws. Impossible results become special values that flow through later calls:
Expression
Result
Why
Math.sqrt(-1)
NaN
No real root; NaN means "not a number"
Math.log(0)
-Infinity
The logarithm has no lower limit
1.0 / 0
Infinity
Double division by zero does not throw
Math.min(-0.0, 0.0)
-0.0
Doubles have a negative zero
NaN is not equal even to itself, so test it with Double.isNaN(x).
Concept
Integer Overflow and the Exact Methods
An int holds -2147483648 to 2147483647. When +, -, or * goes past that range, Java wraps around silently: Integer.MAX_VALUE + 1 is -2147483648. The *Exact methods, such as addExact() and multiplyExact(), throw ArithmeticException instead of returning a wrong number. Even Math.abs(Integer.MIN_VALUE) is still negative.
Reference
The Rounding Family
ceil() rounds up, floor() rounds down, rint() rounds to nearest with .5 ties to even, and round() rounds to nearest with ties toward positive infinity. The first three return a double; round returns a long (an int for a float). For money, prefer whole cents in a long or java.math.BigDecimal: 0.1 + 0.2 prints 0.30000000000000004.
Concept
Degrees vs Radians
sin(), cos(), and tan() expect radians, and the inverse functions return radians. A full circle is 360 degrees or 2 * pi radians. Convert with toRadians() and toDegrees(): Math.sin(30) uses 30 radians; Math.sin(Math.toRadians(30)) uses 30 degrees and gives about 0.5.
Concept
Math.random() vs java.util.Random
random() returns a double from 0.0 up to, but never including, 1.0, handy for a quick dice roll: (int) (Math.random() * 6) + 1. For a repeatable sequence (a seed) or many threads, use a java.util.Random object or ThreadLocalRandom.current().
Code
Java Examples
Example 1 — Circle Area, Rounding, Roots, and Extremes
The most common calls: Math.PI, a power, rounding to cents, a root, a maximum, and an absolute value.
java.lang.Math is a final class with only static methods and two constants, Math.PI and Math.E, for rounding, powers, roots, logarithms, trigonometry, random numbers, and overflow-safe integer math.
No. Every class in java.lang is imported automatically, so Math.sqrt(2.0) works in any Java file.
StrictMath gives bit-for-bit identical results on every computer. Math may use faster processor-specific code for functions like sin or exp, so their last digit can differ between platforms.
Math.round sends an exact .5 tie toward positive infinity and returns a long. Math.rint sends a tie to the even neighbour and returns a double.
Math.PI is only the double closest to pi, so the sine is about 1.2246467991473532E-16 instead of 0. Round the result or compare it with a small tolerance.