Math Class in Java
➤ Math Class Methods
➤ Import Math Class
➤ Math.PI in Java
➤ Math.sqrt() in Java
➤ Math.pow() in Java
➤ Math.max() in Java
➤ Math.abs() in Java
➤ Math.ceil() in Java
➤ Math.floor() in Java
➤ Math.round() in Java
➤ Math.random() Java
Java.lang.Object Class
➤ Object Class in Java
➤ getClass() method
➤ hashCode() Method
➤ toString() Method
➤ Java equals() Method
➤ == vs equals() in Java
➤ HashCode() vs equals()
➤ Clone() Method in Java
➤ Object Class Quiz Set-1
➤ Object Class Quiz Set-2
➤ equals() Method Quiz
➤ Hashcode Value Quiz
Math.pow() Method in Java | To find power value in Java, the pow() method is given in the java.lang.Math class. The pow() method accepts two double-type arguments and returns the value of the first argument raised to the power of the second argument.
Method prototype of java.lang.Math.pow() is:-public static double pow(double a, double b)
Math.pow() Examples
public class Test{
public static void main(String[] args) {
System.out.println(Math.pow(2, 3));
}
}
Output:-
8.0
If we import the Math class statically and then we can invoke the pow() method without calling through its class name.
import static java.lang.Math.*;
public class Test{
public static void main(String[] args) {
System.out.println(pow(2, 3)); // 8
}
}
The “import static java.lang.Math.*;
” statement will import all static members of the Math class. But if we want to import only the sqrt() method of the Math class, not another static method and variables of the Math class then we can use the “import static java.lang.Math.sqrt;” statement. Learn more about static import in Java
import static java.lang.Math.pow;
public class Test{
public static void main(String[] args) {
System.out.println(pow(2, 3)); // 8
}
}
Output:-
8
Program:- Java program to find the area of a circle.
public class CircleArea{
public static void main(String[] args) {
int radius = 10;
// circle area = π * r * r
double area = Math.PI * Math.pow(radius, 2);
// display area
System.out.println(area);
}
}
Output:-
314.1592653589793
Special Cases
Assume x is any number
First argument | Second argument | Return value by pow() |
positive or negative zero | x | 1.0 |
x | 1.0 | x |
x | NaN | NaN |
NaN | Non zero number | NaN |
Math.abs(x) > 1 | positive infinity | positive infinity |
Math.abs(x) < 1 | negative infinity | positive infinity |
Math.abs(x) > 1 | negative infinity | 0.0 |
Math.abs(x) < 1 | positive infinity | 0.0 |
Math.abs(x) = 1 | infinity | NaN |
positive zero | x > 0 | 0.0 |
positive infinity | x < 0 | 0.0 |
positive zero | x < 0 | positive infinity. |
positive infinity | x > 0 | positive infinity. |
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!