The Math.pow() method in Java is used to calculate the power of a number. It takes two arguments, base and exponent, and returns the result as base raised to the power of exponent.
Syntax:
java
double Math.pow(double base, double exponent)
Example:
java
public class PowerExample {
public static void main(String[] args) {
double base = 2;
double exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(base + ” raised to the power of ” + exponent + ” is: ” + result);
}
}
Output:
2.0 raised to the power of 3.0 is: 8.0
Key Points:
– The method returns a double value, even if the inputs are integers.
– It’s part of the java.lang.Math class, so no additional imports are needed.
– Commonly used in mathematical calculations, scientific computations, and algorithms requiring exponentiation.
The Math.pow() method simplifies raising numbers to a power efficiently in Java.