Last Updated: 27 March, 2023
The Java Math class is a built-in class that provides a set of mathematical operations and constants. It is part of the java.lang package, so it is automatically imported when you use Java.
The Math class contains many methods that perform arithmetic, trigonometric, exponential, logarithmic, and other common mathematical functions.
Math class declaration
public final class Math extends Object
Math is a final class, and it extends the Object class.
Note that the methods in the Math class are all static, which means that we don't need to create an instance of the Math class to use them. We can simply call the methods directly by using the class name followed by the method name and the required parameters.
Some of the commonly used methods in the Math class are:
Math.abs() - Returns the absolute value of a number.
Math.ceil() - Returns the smallest integer greater than or equal to the given number.
Math.floor() - Returns the largest integer less than or equal to the given number.
Math.round() - Returns the closest long or int to the given number.
Math.max() - Returns the larger of two numbers.
Math.min() - Returns the smaller of two numbers.
Math.pow() - Returns the value of the first argument raised to the power of the second argument.
Math.sqrt() - Returns the square root of a number.
Math.random() - Returns a random number between 0.0 and 1.0.
In addition to these methods, the Math class also provides several useful constants, such as Math.PI (the value of pi) and Math.E (the value of e).
That's all guys, hope this Java article is helpful for you.
Happy Learning.
feedback@javabytechie.com