The Java Math
class provides more advanced mathematical calculations than what the basic Java math operators provide. The Math
class contains methods for finding the maximum or minimum of two values, rounding values, logarithmic functions, square root, and trigonometric functions (sin, cos, tan etc.).
The Math
is located in the java.lang
package, and not in the java.math
package. Thus, the fully qualified class name of the Math
class is java.lang.Math
.
eg:-
Math.pow()
The Math.pow()
function takes two parameters. The method returns the value of the first parameter raised to the power of the second parameter. Here is a Math.pow()
Java example:
double pow2 = Math.pow(2,2); System.out.println("pow2 = " + pow2); double pow8 = Math.pow(2,8); System.out.println("pow8 = " + pow8);
The output from this Math.pow()
example would be:
pow2 = 4.0 pow8 = 256.0
In other words, the Math.pow()
example calculate the values of 22 and 28
which are 4 and 256.