Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Math pow() Method

The pow() method(short for power) is used to find the value of one number raised to the power of some other number. It is part of the Math class of java.lang package. It returns a double value.

Explicit casting is required if we want to convert the output to some other type. The general signature of the method is shown below.

public static double pow(double base, double power)

Examples: Java pow() Method

Let's take a look at a few examples to understand its working.

import java.lang.Math;
public class PowDemo
{
	public static void main(String[] args)
	{
		double d1 = Math.pow(3, 5);
		double d2 = Math.pow(3.5, 1.5);
		int i = (int) Math.pow(3.5, 1.5);//Casting is required for int type
		
		System.out.println("3 raised to the power 5: " + d1);
		System.out.println("3.5 raised to the power 1.5: " + d2);
		System.out.print("3.5 raised to the power 1.5(for int type): " + i);
	}
}


3 raised to the power 5: 243.0
3.5 raised to the power 1.5: 6.547900426854397
3.5 raised to the power 1.5(for int type): 6

We can also use it to find the square roots or cube roots of the numbers.

import java.lang.Math;
public class PowDemo
{
	public static void main(String[] args)
	{
		double squareRoot = Math.pow(16, 0.5);
		double cubeRoot = Math.pow(27, (1.0 / 3));
		
		System.out.println("Square root of 16 is: " + squareRoot);
		System.out.print("Cube root of 27 is: " + cubeRoot);
	}
}


Square root of 16 is: 4.0
Cube root of 27 is: 3.0

Special Cases with pow() Method

There are a few special cases that are worth mentioning.

  • If the second parameter(the power) is positive or negative zero, then it returns 1.0.
  • If the second parameter is 1.0, then it simply returns the first parameter.
  • If the second parameter is NaN(not a number), then it returns NaN.
  • If the first parameter is NaN and the second parameter is non-zero, then it returns NaN.
  • If the first parameter is NaN but the second parameter is zero, then it returns 1.
import java.lang.Math;
public class PowDemo
{
	public static void main(String[] args)
	{
		System.out.println("When Second parameter is zero:");
		System.out.println(Math.pow(5.0, 0.0));
		System.out.println(Math.pow(5.0, -0.0));
		
		System.out.println("When Second parameter is one:");
		System.out.println(Math.pow(5.0, 1));
		
		System.out.println("When Second parameter is NaN:");
		System.out.println(Math.pow(5.0, Double.NaN));
		
		System.out.println("When first parameter is NaN and second parameter is non-zero:");
		System.out.println(Math.pow(Double.NaN, 14));
		
		System.out.println("When first parameter is NaN and second parameter is zero:");
		System.out.println(Math.pow(Double.NaN, 0));
	}
}


When Second parameter is zero:
1.0
1.0
When Second parameter is one:
5.0
When Second parameter is NaN:
NaN
When first parameter is NaN and second parameter is non-zero:
NaN
When first parameter is NaN and second parameter is zero:
1.0

There are a few more special cases. Simple exponential math is used to understand the output. For example, we know that the square root of -1 is not a real number. We should get the NaN value if we try to do this with pow().

import java.lang.Math;
public class PowDemo
{
	public static void main(String[] args)
	{
		System.out.print("Square root of -1: " + Math.pow(-1, 0.5));
	}
}


Square root of -1: NaN

We also know that a positive value raised to infinity will tend to positive infinity. And a positive value, when raised to negative infinity, tends to be zero. This is also replicated by the pow() method.

import java.lang.Math;
public class PowDemo
{
	public static void main(String[] args)
	{
		System.out.println("Positive Value raised to Infinity: " + Math.pow(5, Double.POSITIVE_INFINITY));
		System.out.print("Positive Value raised to Negative Infinity: " + Math.pow(5, Double.NEGATIVE_INFINITY));
	}
}


Positive Value raised to Infinity: Infinity
Positive Value raised to Negative Infinity: 0.0

Summary

In this short tutorial, we learned how to use the pow() method of the java.lang.Math class to raise a number to a given power. We also looked at some special cases that may occur when using this method.