English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Writer

Java other topics

Java Math pow() usage and example

Java Math Mathematical Methods

Java Math pow() method to calculate power (a to the power of b).

That is, pow(a, b) = ab

The syntax of pow() method is:

Math.pow(double num1, double num2)

Note: pow() is a static method. Therefore, we can use the class name to access this method Math.

pow() parameter

  • num1 - Basic parameter

  • num2 - Exponent parameter

The return value of pow()

  • The returned result num1num2

  • If num2If zero, then return 1.0

  • If num1If zero, then return 0.0

Example: Java Math pow()

class Main {
  public static void main(String[] args) {
    //Create double precision variable
    double num1 = 5.0;
    double num2 = 3.0;
    // With positive number Math.pow()
    System.out.println(Math.pow(num1, num2));  // 125.0
    //With zero Math.pow()
    double zero = 0.0;
    System.out.println(Math.pow(num1, zero));    // 0.0
    System.out.println(Math.pow(zero, num2));    // 1.0
    //With infinite Math.pow()
    double infinity = Double.POSITIVE_INFINITY;
    System.out.println(Math.pow(num1, infinity));  // Infinity
    System.out.println(Math.pow(infinity, num2));  // Infinity
    //Negative Number Math.pow()
    System.out.println(Math.pow(-num1, -num2));    // 0.008
  }
}

In the above example, we used Math.pow() withpositive numbers,negative numbers,zeroandInfinite.

Here, Double.POSITIVE_INFINITY is used to implement positive infinity in the program.

When we pass an int value to the pow() method, it automatically converts the int value to the double value.

int a = 2;
int b = 5;
Math.pow(a, b);   // Return 32.0

Recommended Tutorials

Java Math Mathematical Methods