Java.lang Package Classes

Java Math - pow() Method



The java.lang.Math.pow() method returns the first argument raised to the power of the second argument. In special cases it returns the following:

  • If the second argument is positive or negative zero, then the result is 1.0.
  • If the second argument 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 second argument is 1.0, then the result is the same as the base.

Syntax

public static double pow(double a, double b)

Parameters

a Specify the base.
b Specify the exponent.

Return Value

Returns the first argument raised to the power of the second argument.

Exception

NA.

Example:

In the example below, pow() method is used to calculate the first argument raised to the power of the second argument.

import java.lang.*;

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.lang - Math