Java Math ClassLast Updated : 7 Jan 2026 The Java Math class is a fundamental part of the Java language's standard library, offering a wide range of mathematical functions. It provides static methods for performing basic arithmetic operations like min(), max(), random(), round() etc. Additionally, it offers methods for more complex operations such as calculating logarithms and trigonometric functions. Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc. Unlike some of the StrictMath class numeric methods, all implementations of the equivalent function of Math class can't define to return the bit-for-bit same results. This relaxation permits implementation with better-performance where strict reproducibility is not required. Importing Java Math ClassMath class is part of the java.lang package, which is imported automatically, so you can use its methods without explicitly importing it. Syntax to use a Math methodThe syntax to se a Math class's method is as follows: Example SyntaxThe following statements demonstrates how you can use the sqrt() and pow() methods of math class: Java Math Class MethodsOne of the key features of the Math class is its support for working with floating-point numbers. These methods can handle a variety of mathematical tasks involving decimals, fractions, and very large or very small numbers. For example, the Math.round() method can round a floating-point number to the nearest integer, while Math.random() can generate a random number between 0.0 and 1.0. The Math class also provides methods for working with angles and trigonometric functions. It includes methods for calculating sine, cosine, and tangent, as well as their inverse functions. These functions are useful for tasks like calculating distances and angles in geometry, or for simulating natural phenomena in physics and engineering. Basic Math MethodsThe following table shows different basic math methods in Java.
Logarithmic Math MethodsThe following table shows different Logarithmic math methods in Java.
Trigonometric Math MethodsThe following table shows different Trigonometric math methods in Java.
Hyperbolic Math MethodsThe following table shows different hyperbolic math methods in Java.
Angular Math MethodsThe following table shows different angular math methods in Java.
Examples of Math Class MethodsSome useful methods of the Java Math class with practical examples. Example 1: Basic Math OperationsThe following example demonstrates basic mathematical operations such as maximum, square root, power, and logarithmic calculations using the Math class. Output: Maximum number of x and y is: 28.0 Square root of y is: 2.0 Power of x and y is: 614656.0 Logarithm of x is: 3.332204510175204 Logarithm of y is: 1.3862943611198906 log10 of x is: 1.4471580313422192 log10 of y is: 0.6020599913279624 log1p of x is: 3.367295829986474 exp of a is: 1.446257064291475E12 expm1 of a is: 1.446257064290475E12 Example 2: Trigonometric MethodsThe following example demonstrates how to use trigonometric and hyperbolic functions in Java using the Math class. Output: Sine value of a is: -0.9880316240928618 Cosine value of a is: 0.15425144988758405 Tangent value of a is: -6.405331196646276 Sine value of a is: NaN Cosine value of a is: NaN Tangent value of a is: 1.5374753309166493 Sine value of a is: 5.343237290762231E12 Cosine value of a is: 5.343237290762231E12 Tangent value of a is: 1.0 Example 3: Complete Math Class ReferenceThe following example demonstrates multiple functionalities of the Math class, including power, logarithms, trigonometric functions, rounding, random numbers, hypotenuse calculation, and constants. Output: Addition: 32.0 Subtraction: 24.0 Multiplication: 112.0 Division: 7.0 Square root of 28.0: 5.291502622129181 Cube root of 28.0: 3.0365889718756627 Power of 28.0 to 4.0: 614656.0 Sine of 45.0 degrees: 0.7071067811865475 Cosine of 45.0 degrees: 0.7071067811865476 Tangent of 45.0 degrees: 0.9999999999999999 Absolute value of -123.456: 123.456 Ceil value of -123.456: -123.0 Floor value of -123.456: -124.0 Round value of -123.456: -123 Random number between 0.0 and 1.0: 0.40493356810101455 Random number between 0 and 100: 61 Maximum value: 30.9 Minimum value: 5.2 e^28.0: 1.446257064291475E12 Logarithm base 10 of 28.0: 1.4471580313422192 Logarithm base e of 28.0: 3.332204510175204 Hypotenuse of a right triangle with sides 3.0 and 4.0: 5.0 Arcsine of 0.5: 30.000000000000004 Arccosine of 0.5: 60.00000000000001 Arctangent of 0.5: 26.56505117707799 Value of PI: 3.141592653589793 Value of E: 2.718281828459045 Next TopicWrapper class in Java |
We request you to subscribe our newsletter for upcoming updates.