Math.pow() Method in Java

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 argumentSecond argumentReturn value by pow()
positive or negative zerox1.0
x1.0x
xNaNNaN
NaNNon zero numberNaN
Math.abs(x) > 1positive infinitypositive infinity
Math.abs(x) < 1negative infinitypositive infinity
Math.abs(x) > 1negative infinity0.0
Math.abs(x) < 1positive infinity0.0
Math.abs(x) = 1infinityNaN
positive zerox > 00.0
positive infinityx < 00.0
positive zerox < 0positive infinity.
positive infinityx > 0positive 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!

Leave a Comment

Your email address will not be published. Required fields are marked *