The java.lang.Math class provides fast, hardware-accelerated static methods for elementary numeric operations including rounding, clamping, square roots, exponential powers, logarithms, and circular/hyperbolic trigonometry.
Zero Imports Required: Because Math resides in java.lang, all of its methods and constants (Math.PI and Math.E) are available globally without an import statement.
java.lang.Math defines two public static final double constants at 64-bit IEEE 754 precision:
System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.E); // 2.718281828459045Terminal Output:
3.141592653589793
2.718281828459045
Core arithmetic utilities for absolute magnitude, ceiling/floor quantization, integer floor division, and uniform random sampling:
| Method Tutorial | Return Type | Description & Rounding Rule |
|---|---|---|
Math.abs(x) |
int, long, float, double |
Returns non-negative absolute magnitude |x|. |
Math.ceil(x) |
double |
Smallest integer >= x (rounds toward +Infinity). |
Math.floor(x) |
double |
Largest integer <= x (rounds toward -Infinity). |
Math.floorDiv(x, y) |
int, long |
Integer quotient rounded toward -Infinity. |
Math.round(x) |
int, long |
Rounds to nearest integer via floor(x + 0.5). |
Math.min(a, b) |
int, long, float, double |
Returns the smaller of two numbers. |
Math.max(a, b) |
int, long, float, double |
Returns the larger of two numbers. |
Math.random() |
double |
Uniform pseudorandom number in [0.0, 1.0). |
Math.sqrt(x) |
double |
Non-negative square root of x (NaN if x < 0). |
System.out.println(Math.abs(-42)); // 42
System.out.println(Math.ceil(4.2)); // 5.0
System.out.println(Math.floor(4.8)); // 4.0
System.out.println(Math.round(4.5)); // 5
System.out.println(Math.sqrt(81.0)); // 9.0Terminal Output:
42
5.0
4.0
5
9.0
Continuous growth, arbitrary exponents, and base-e / base-10 logarithms:
| Method Tutorial | Mathematical Formula | Primary Engineering Use Case |
|---|---|---|
Math.pow(a, b) |
a^b |
Raises base a to exponent b. |
Math.exp(x) |
e^x |
Euler's number raised to power x. |
Math.log(x) |
ln(x) (base e) |
Natural logarithm of x; inverse of Math.exp(x). |
Math.log10(x) |
log10(x) (base 10) |
Common base-10 logarithm (decibels, digit counts). |
Math.log1p(x) |
ln(1 + x) |
High-precision natural log of 1 + x near zero. |
System.out.println(Math.pow(2.0, 10.0)); // 1024.0
System.out.println(Math.log(Math.E)); // 1.0 (base e)
System.out.println(Math.log10(1000.0)); // 3.0 (base 10)
System.out.println(Math.log1p(1e-12)); // 9.999999999995E-13Terminal Output:
1024.0
1.0
3.0
Circular trigonometric functions operate in radians. Use Math.toRadians() and Math.toDegrees() to convert between degrees and radians:
| Method Tutorial | Domain / Output | Role & Unit Convention |
|---|---|---|
Math.toRadians(deg) |
Degrees → Radians | Multiplies degree input by Math.PI / 180.0. |
Math.toDegrees(rad) |
Radians → Degrees | Multiplies radian input by 180.0 / Math.PI. |
Math.sin(rad) |
Radians → [-1, 1] |
Circular trigonometric sine of a radian angle. |
Math.cos(rad) |
Radians → [-1, 1] |
Circular trigonometric cosine of a radian angle. |
Math.tan(rad) |
Radians → (-inf, +inf) |
Circular trigonometric tangent (sin(rad) / cos(rad)). |
Math.asin(v) |
[-1, 1] → [-pi/2, pi/2] |
Inverse sine (arc sine) in radians. |
Math.acos(v) |
[-1, 1] → [0, pi] |
Inverse cosine (arc cosine) in radians. |
Math.atan(v) |
(-inf, +inf) → [-pi/2, pi/2] |
Inverse tangent (arc tangent) in radians. |
Math.sinh(x) |
Real → (-inf, +inf) |
Hyperbolic sine: (e^x - e^-x) / 2. |
Math.cosh(x) |
Real → [1.0, +inf) |
Hyperbolic cosine: (e^x + e^-x) / 2. |
Math.tanh(x) |
Real → [-1.0, 1.0] |
Hyperbolic tangent: sinh(x) / cosh(x). |
System.out.println(Math.round(Math.cos(Math.toRadians(60.0)) * 10) / 10.0); // 0.5
System.out.println(Math.round(Math.toDegrees(Math.acos(0.5)))); // 60
System.out.println(Math.tanh(0.0)); // 0.0Terminal Output:
0.5
60
0.0
Standard Java +, -, and * operators wrap silently on overflow. Use Math.addExact(), Math.subtractExact(), and Math.multiplyExact() to throw an ArithmeticException on overflow:
try {
Math.addExact(Integer.MAX_VALUE, 1);
} catch (ArithmeticException ex) {
System.out.println("Caught: " + ex.getMessage());
}Terminal Output:
Caught: integer overflow
Combining Math.sqrt(), Math.pow(), Math.atan(), and Math.toDegrees() to compute polar coordinates:
public class Vector2DAnalyzer {
public static void main(String[] args) {
double mag = Math.sqrt(Math.pow(3.0, 2) + Math.pow(4.0, 2));
double deg = Math.toDegrees(Math.atan(4.0 / 3.0));
System.out.println(String.format("Mag: %.1f | Deg: %.2f", mag, deg));
}
}Terminal Output:
Mag: 5.0 | Deg: 53.13
Calling Math.sin(90) computes the sine of 90 radians (0.89399), not 90 degrees! Convert degree inputs first via Math.sin(Math.toRadians(90)).
In Math.ceil(7 / 2), integer division truncates 7 / 2 to 3 before ceil() runs, returning 3.0 instead of 4.0. Use Math.ceil((double) a / b).
Compile and run this verification suite in any Java environment:
public class JavaMathMasterDemo {
public static void main(String[] args) {
if (Math.abs(-15) == 15 && Math.min(4, 9) == 4 && Math.max(4, 9) == 9) {
System.out.println("Check 1 Passed: Magnitude and clamping verified.");
}
if (Math.log10(100.0) == 2.0 && Math.sqrt(49.0) == 7.0) {
System.out.println("Check 2 Passed: Logarithms and roots verified.");
}
System.out.println("All java.lang.Math master verification checks passed!");
}
}Terminal Output:
Check 1 Passed: Magnitude and clamping verified.
Check 2 Passed: Logarithms and roots verified.
All java.lang.Math master verification checks passed!
StrictMath guarantees bit-for-bit identical results across all JVM platforms using software FDLIBM algorithms, whereas Math allows JVM intrinsics to use hardware CPU instructions within 1 ULP accuracy.
Math.log() computes the natural logarithm (base e), whereas Math.log10() computes the common base-10 logarithm with exact integer results on powers of 10.
No. java.lang.Math is a final class with a private constructor; all methods are invoked statically.
Use the change-of-base identity with Math.log(): Math.log(x) / Math.log(2.0).
MIN_VALUE overflow handling.String methods.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.