Java Math.pow() Method with Examples

Calculating the power of some number (Java math pow method)

Now, let’s have a look at the method with which, we can calculate something to the power of something else. This is like if we want to calculate 2 to the power 3, this can be easily done with this method. So let’s have a look at the implementation part. Note that this method takes two double numbers as arguments, out of which the first number is the base and another number is the power. Also note that it returns the double value, which is the given power value of that particular number.

import java.lang.Math;
public class UnderstandingMath {
public static void main(String[] args) {
System.out.println(Math.pow(2, 3));
}
}

In the above program, the output comes out to be 8.0, which is 2 to power 3. So, we can easily calculate the power of a number. You can try out some more examples to get a better understanding of the things.

Some Special cases –

  • If the second argument of this method is zero(positive or negative), the output is 1.0
    190
  • If the second argument of this method is 1.0, then the output is the same as the first argument.
  • If the second argument is NaN, the output is NaN.
  • If the first argument is NaN, and the second argument is a non-zero number, the output is NaN.

We have discussed a few special cases for this method. However, there are some more special cases, which you can explore in the docs as well.