Math Methods

Anonymous contributor's avatar
Anonymous contributor
Published Aug 5, 2021Updated Oct 23, 2023
Contribute to Docs

The Java Math class provides several static methods that allows us to work on mathematical calculations with numbers. These methods can be accessed by using the Math class with a given method:

Math.method();

Math Constants

There are several mathematical constants defined in the Java Math class. These constants are marked as public static final, which means they are public, class-level constants with unchangeable values and can be accessed as follows:

Name value Definition
Math.PI 3.141592653589793 This constant represents the mathematical constant π (pi).
Math.E 2.718281828459045 This constant represents the base of the natural logarithm, also known as Euler’s number.

Math Methods

Here are the Math methods in alphabetical order:

Math Methods

.abs()
Returns the absolute value of the argument.
.acos()
Returns the inverse cosine of the argument in radians.
.addExact()
Returns the sum of its arguments.
.asin()
Returns the inverse sine of the argument in radians.
.atan()
Returns the inverse tangent of the argument in radians.
.atan2()
Returns the counterclockwise angle, in radians, between a (x,y) point and the positive x-axis.
.cbrt()
Returns the cube root of a double-type value.
.ceil()
Returns the double value that is a mathematical integer and is greater than or equal to the original value.
.cos()
Returns the trigonometric cosine of the specified angle.
.decrementExact()
Returns the argument decremented by one, throwing an exception if the result overflows the datatype.
.floor()
Returns the largest integer value that is less than or equal to the argument. When the provided value is either an integer, zero, not a number, or positive or negative infinity.
.hypot()
Returns the hypotenuse of a right-angled triangle.
.incrementExact()
Returns the argument incremented by one, throwing an exception if the result overflows the datatype.
.log()
Returns the natural logarithm (base e) of a double value as a parameter.
.log10()
Returns the logarithm base 10 of a double value as a parameter.
.log1p()
Returns the natural logarithm (base e) of the sum of a double value as a parameter and 1.
.max()
Returns the maximum value from the given two arguments.
.min()
Returns the minimum value from the given two arguments.
.multiplyExact()
Returns the product of its arguments.
.negateExact()
Returns the negation of the argument and raises an exception if the result overflows.
.nextAfter()
Returns the floating-point number next to the first argument in the direction of the second argument.
.nextDown()
Returns the floating-point value adjacent to the parameter provided in the direction of negative infinity.
.pow()
Returns the first argument raised to the power of the second argument.
.random()
Returns a pseudorandom double that is 0.0 or greater, and less than 1.0.
.rint()
Returns a double that is rounded to the closest whole integer.
.round()
Returns an int or long value that is closest to the number provided.
.scalb()
Returns the first argument multiplied by 2 to the power of the second argument.
.signum()
Used to determine whether a number is positive, negative, zero, or NaN.
.sin()
Returns the trigonometric sine of the specified angle.
.sinh()
Returns the hyperbolic sine of a double-type value.
.sqrt()
Returns the positive, properly rounded square root of a double-type value.
.tan()
Returns the tangent of an angle given in radians.
.tanh()
Returns the hyperbolic sine of a double-type value.
.toDegrees()
Returns an angle measurement converted from radians to degrees.
.toIntExact()
Returns the integer value of a long type argument. An exception is thrown if the value of argument overflows that of an int.
.toRadians()
Converts an angle in degrees to an approximately equivalent angle in radians.
.ulp()
Returns the size of the unit of last precision of a number.

All contributors

Contribute to Docs

Learn Java on Codecademy