In Java, the Math class provides many built-in methods to perform mathematical operations such as square roots, powers, rounding, absolute values, and trigonometric calculations. The Math class belongs to java.lang package, so you do not need to import it explicitly.
All methods in the Math class are static, which means you can call them directly using Math.methodName().
These methods return the maximum or minimum value between two numbers.
The sqrt() method returns the square root of a number.
The pow() method returns the value of a number raised to the power of another number.
The abs() method returns the absolute (positive) value of a number.
The random() method generates a random number between 0.0 (inclusive) and 1.0 (exclusive).
To generate a random number within a range:
The round() method rounds a number to the nearest integer.
ceil() rounds up to the nearest integer, while floor() rounds down.
The cbrt() method returns the cube root of a number.
The Math class also provides trigonometric methods like sin(), cos(), and tan(). These methods use radians as input.
The Math class also provides important constants:
Helpful Tip: Always use built-in Math methods instead of writing complex mathematical logic manually. They are optimized and reliable.