In Java, Math.pow(a, b) returns a raised to the power b, as a double. Math.pow(2, 10) is 1024.0 and Math.pow(9, 0.5) is 3.0.
It is the go-to method for growth formulas such as compound interest, for areas and volumes with a squared or cubed value, and for roots written as fractional exponents. It has been part of java.lang.Math since the early versions of Java.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.pow(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the base first and the exponent second. Both arguments are widened to double, and the result is always a double.
Java
double a = Math.pow(2, 10); // 1024.0
double b = Math.pow(2, -1); // 0.5
int kb = (int) Math.pow(2, 10); // 1024 (cast needed)
double total = money * Math.pow(1 + rate, years); // compound interest
Call
What it does
Math.pow(10, 2)
Ten squared: returns 100.0.
Math.pow(9, 0.5)
Exponent 0.5 is a square root: returns 3.0.
(int) Math.pow(2, 8)
Whole-number power as an int: 256.
Math.pow(x, 0)
Anything to the power 0 is 1.0, even NaN.
For squaring in a loop, x * x is shorter and faster than Math.pow(x, 2), and it stays an int when x is an int. Use pow when the exponent is a variable or a fraction.
Library declarations
Java
// java.lang.Math
public static double pow(double a, double b)
Parameters
Parameter
Description
a
The base. int, long, and float values are widened to double.
b
The exponent, which may be negative or fractional.
Return Value
Return Value
Description
double
a raised to the power b. It can be NaN, Infinity, or 0.0 for special or extreme inputs.
Example 1 — Whole, Negative, and Fractional Exponents
Try a few exponents and turn the double result into an int with a cast.
Java
public class MathPowBasics {
public static void main(String[] args) {
System.out.println("2 to the 10 = " + Math.pow(2, 10));
System.out.println("2 to the -1 = " + Math.pow(2, -1));
System.out.println("9 to the 0.5 = " + Math.pow(9, 0.5));
System.out.println("10 squared = " + Math.pow(10, 2));
int kilobyte = (int) Math.pow(2, 10);
System.out.println("As an int: " + kilobyte);
}
}
Output
2 to the 10 = 1024.0
2 to the -1 = 0.5
9 to the 0.5 = 3.0
10 squared = 100.0
As an int: 1024
How It Works
Doubles all the way. The int arguments 2 and 10 are widened to double, so even a whole-number answer prints with .0: 1024.0.
Negative and fractional exponents. A negative exponent means "one divided by", so 2 to the -1 is 0.5; the exponent 0.5 means square root, so 9 gives 3.0.
Cast when you need an int.(int) drops the .0 and stores the plain integer 1024, which is safe here because the power is an exact whole number.
Example 2 — Circle Area and Compound Interest
Square a radius for the area of a circle, then grow a savings balance with (1 + rate) raised to the number of years.
Java
import java.util.Locale;
public class MathPowGrowth {
public static void main(String[] args) {
double radius = 5;
double area = Math.PI * Math.pow(radius, 2);
System.out.println("Circle area: " + Math.round(area * 100) / 100.0);
double principal = 1200;
double rate = 0.04;
int years = 5;
double total = principal * Math.pow(1 + rate, years);
System.out.println("After " + years + " years: " + String.format(Locale.US, "%.2f", total));
}
}
Output
Circle area: 78.54
After 5 years: 1459.98
How It Works
Squared radius.Math.pow(radius, 2) is 25.0, so the area is about 25 * 3.14159 = 78.5398, which Math.round(area * 100) / 100.0 trims to 78.54.
Yearly growth factor. A 4 percent rate means the balance is multiplied by 1.04 each year, so after 5 years it is multiplied by Math.pow(1.04, 5), about 1.2167.
Display with two decimals. 1200 times 1.2167 is about 1459.98348; String.format with Locale.US prints it as 1459.98 using a dot, whatever the computer's language setting.
Example 3 — Zero, NaN, Negative Bases, and Overflow
The edge cases that surprise beginners, including what happens when you cast a huge power to int.
Java
public class MathPowSpecial {
public static void main(String[] args) {
System.out.println("0 to the 0: " + Math.pow(0, 0));
System.out.println("NaN to the 0: " + Math.pow(Double.NaN, 0));
System.out.println("-8 to the 1/3: " + Math.pow(-8, 1.0 / 3));
System.out.println("0 to the -1: " + Math.pow(0, -1));
System.out.println("10 to the 400: " + Math.pow(10, 400));
System.out.println("2^31 as int: " + (int) Math.pow(2, 31));
}
}
Output
0 to the 0: 1.0
NaN to the 0: 1.0
-8 to the 1/3: NaN
0 to the -1: Infinity
10 to the 400: Infinity
2^31 as int: 2147483647
How It Works
Exponent zero wins. Java defines any base to the power 0 as 1.0, including 0 and NaN, because the answer does not depend on the base.
No real answer. The exponent 1.0 / 3 is not a whole number, and a negative base has no real result for it, so the result is NaN; Math.cbrt handles this case.
Too big for the type. 10 to the 400 exceeds the largest double, giving Infinity; 2 to the 31 is exact, but the (int) cast stops at Integer.MAX_VALUE (2147483647) instead of wrapping around.
Frequently Asked Questions
It raises the first argument to the power of the second and returns the result as a double. Math.pow(2, 10) returns 1024.0.
Because it must also handle fractional and negative exponents, such as Math.pow(2, -1), which is 0.5. Cast with (int) when you know the result is a whole number that fits, for example (int) Math.pow(2, 10).
Write x * x. It is shorter, faster, and keeps int values as int. Math.pow(x, 2) works too but returns a double.
It is 1.0. Java follows the convention that any value to the power 0 is 1.0, even NaN.
A negative base with a non-whole exponent, such as Math.pow(-8, 1.0 / 3), has no real result, so Java returns NaN. For cube roots use Math.cbrt, which accepts negative numbers.