Java Tutorial Java Advanced Java References

Java Math - pow() Method



The Java pow() method returns the base raise to the power of exponent. In special cases it returns the following:

  • If the exponent is positive or negative zero, then the result is 1.0.
  • If the exponent is NaN, then the result is NaN.
  • If the first argument is NaN and the second argument is nonzero, then the result is NaN.
  • If the exponent is 1.0, then the result is the same as the base.

Syntax

public static double pow(double base, double exponent)

Parameters

base Specify the base.
exponent Specify the exponent.

Return Value

Returns the base raise to the power of exponent.

Exception

NA.

Example:

In the example below, pow() method is used to calculate the base raised to the power of exponent.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.pow(2, 3)); 
  System.out.println(Math.pow(2.3, 4));   
 }
}

The output of the above code will be:

8.0
27.98409999999999

❮ Java Math Methods