Math Class in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The Math class in Java includes methods for executing fundamental mathematical operations such as square root, logarithm, exponential, and trigonometric functions.

In this article, we will be reading about the Math class in Java and the various methods defined in it.

Introduction

Various techniques for working with numerical calculations are offered by the Java Math class, including min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), and abs() methods.

Only when a certain minimum or maximum value is present, does overflow occur. When performing mathematical operations such as incrementing, decrementing, dividing, finding absolute values, or negating numbers, it's crucial to ensure they stay within the appropriate maximum and minimum boundaries.

When using the addExact(), subtractExact(), multiplyExact(), and toIntExact() methods, an ArithmeticException is thrown if the resultant value exceeds the allowable range for the data type (int or long).

Declaration of Math Class in Java

The declaration for the Math class in Java is as follows:

Field of Math Class in Java

Following are the fields for the Math class in Java

Static Double E

This is the decimal number that sits closest to the value of 'e,' the base used in natural logarithms.

Static Double PI

This double value contains the value of π, i.e. the ratio of the circle's circumference to its diameter.

Methods of Math Class in Java

The Math class in Java contains numerous methods to perform various kinds of math operations including basic math, trigonometric, logarithmic, operations. Let's have a look at some of these.

Basic Math Methods

NameDescription
Math.signum()Returns the sign of the value
Math.abs() Returns absolute value of the value in the parentheses
Math.copySign()Returns the absolute value of the first argument along with the sign in the second argument
Math.round()Returns value rounded to the nearest value
Math.max()Returns largest of the two values
Math.min()Returns minimum of the 2 values
Math.ceil()Returns the smallest integer greater than or equal to the value in the parentheses
Math.floor()Returns the largest integer smaller than or equal to the value in the parentheses
Math.pow()Returns the value of the first argument raised to the power given in the second argument
Math.sqrt()Returns square root of the number
Math.cbrt()Returns cube root of the number
Math.random()Returns a positive double value between 0.0 and 1.0
Math.hypot()Returns the hypotenuse value (i.e. square root of the sum of x squared and y squared) without intermediate overflow or underflow.

Logarithmic Math Methods

NameDescription
Math.log()Returns the natural logarithm of a double value
Math.log10()Returns the base 10 logarithm of a double value.
Math.log1p()Returns the natural logarithm of the sum of the argument and 1.
Math.exp()Returns E (Euler's Number = 2.71828) raised to power of a double value
Math.expm1()Returns power of E after subtracting one from it

Trigonometric Math Methods

NameDescription
Math.sin()Returns sine value of the number
Math.cos()Returns cosine value of the number
Math.tan()Returns tangent value of the number
Math.asin()Returns arc sine value of the number
Math.acos()Returns arc cosine value of the number
Math.atan()Returns arc tangent value of the number

Hyperbolic Math Methods

NameDescription
Math.sinh()Returns hyperbolic sine value of the number
Math.cosh()Returns hyperbolic cosine value of the number
Math.tanh()Returns hyperbolic tangent value of the number

Angular Math Methods

NameDescription
Math.toDegreesConverts radian angle to angle in degrees
Math.toRadiansConverts degrees angle to radian angle

Examples of Math Class in Java

Example 1: A few basic mathematic operations

Output:

Example 2: A few logarithmic operations

Output:

Conclusion

  • The Math class in Java provides methods that are used to perform numerical computations including basic mathematical, logarithmic, exponential, trigonometric, hyperbolic, angular, etc.
  • The Math class in Java has 2 fields - static double E and static double PI which store approximate values of e and π respectively.