The Math.pow(a, b) method returns the value of the first argument (a, the base) raised to the power of the second argument (b, the exponent).
Universal Floating-Point Exponentiation: Supports positive, negative, and fractional exponents as 64-bit double values. For dedicated square roots, see Math.sqrt(). For natural base-e powers, explore Math.exp(). For inverse logarithmic operations, see Math.log() and Math.log10().
Defined in java.lang.Math:
// Formal signature of Math.pow() in java.lang.Math
public static double pow(double a, double b)
| Parameter / Element | Type | Description & Operational Rule |
|---|---|---|
a (First Argument) |
double |
The base number to be multiplied. |
b (Second Argument) |
double |
The exponent to which the base a is raised. |
| Return Value | double |
The computed value a^b as a 64-bit floating-point double. |
Evaluating standard powers, negative exponents (a^-b = 1 / a^b), and signed bases:
// Positive, negative, and zero exponents (a^b)
System.out.println(Math.pow(2.0, 4.0)); // 16.0 (2^4)
System.out.println(Math.pow(5.0, 0.0)); // 1.0 (5^0)
System.out.println(Math.pow(2.0, -3.0)); // 0.125 (1 / 2^3)
System.out.println(Math.pow(-2.0, 3.0)); // -8.0 (odd integer power of negative)Terminal Output:
16.0
1.0
0.125
-8.0
Fractional exponents compute roots (e.g., 0.5 for square root). Raising a negative base to a non-integer exponent returns Double.NaN:
// Fractional exponents (roots) and negative base rules
System.out.println(Math.pow(25.0, 0.5)); // 5.0 (square root: 25^(1/2))
System.out.println(Math.pow(27.0, 1.0 / 3.0)); // 3.0 (cube root: 27^(1/3))
// Negative base with non-integer exponent is undefined in real numbers -> NaN
System.out.println(Math.pow(-2.12, -5.67)); // NaNTerminal Output:
5.0
3.0
NaN
Under IEEE 754 rules, any number raised to 0.0 equals 1.0, and 1.0 raised to any exponent equals 1.0—even when the other parameter is NaN or Infinity:
System.out.println(Math.pow(0.0, 0.0)); // 1.0
System.out.println(Math.pow(Double.NaN, 0.0)); // 1.0 (NaN^0)
System.out.println(Math.pow(1.0, Double.NaN)); // 1.0 (1^NaN)Terminal Output:
1.0
1.0
1.0
Unlike Python's ** or calculator notation, the ^ symbol in Java is the bitwise XOR operator, not an exponentiation operator:
int xorResult = 2 ^ 3; // Bitwise XOR (010 ^ 011 = 1)
double powResult = Math.pow(2, 3); // 2.0^3.0 = 8.0
System.out.println("XOR: " + xorResult + " | POW: " + powResult);Terminal Output:
XOR: 1 | POW: 8.0
When to use faster or higher-precision alternatives instead of general-purpose Math.pow():
| Target Calculation | General Math.pow() | Optimal Alternative | Why the Alternative Wins |
|---|---|---|---|
| Squaring / Cubing | Math.pow(x, 2) |
x * x |
10x+ faster CPU multiplication; no float conversion. |
| Square Root | Math.pow(x, 0.5) |
Math.sqrt(x) |
Hardware FSQRT instruction; strictly rounded. |
| Power of 2 (Integer) | (long) Math.pow(2, n) |
1L << n (for n < 63) |
Single-cycle bit shift; exact up to 62 bits. |
| Base e Power | Math.pow(Math.E, x) |
Math.exp(x) |
Avoids rounding error in the base constant Math.E. |
Computing compound interest over discrete compounding periods (monthly, quarterly, or annually):
public class CompoundInterestCalculator {
public static double futureValue(double principal, double rate, int timesPerYear, int years) {
// Formula: A = P * (1 + r/n)^(n*t)
return principal * Math.pow(1.0 + (rate / timesPerYear), timesPerYear * years);
}
public static void main(String[] args) {
double amount = futureValue(10000.0, 0.06, 12, 5);
System.out.println(String.format("%.2f", amount)); // 13488.50
}
}Terminal Output:
13488.50
Scaling retry intervals exponentially (`100ms, 200ms, 400ms, 800ms`) for resilient HTTP clients:
public class ExponentialBackoff {
public static long retryDelayMs(long baseMs, int attempt) {
return (long) (baseMs * Math.pow(2.0, attempt));
}
public static void main(String[] args) {
System.out.println(retryDelayMs(100, 0) + " ms"); // 100 ms
System.out.println(retryDelayMs(100, 3) + " ms"); // 800 ms
}
}Terminal Output:
100 ms
800 ms
A 64-bit double only has 53 bits of significand precision. For exact integer powers above 9,007,199,254,740,992 (2^53), use BigInteger.valueOf(base).pow(exp).
Writing Math.pow(27, 1 / 3) evaluates 1 / 3 as integer 0, returning 27^0 = 1.0! Always use floating-point literals: Math.pow(27, 1.0 / 3.0) (or Math.cbrt(27)).
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathPowFullDemo {
public static void main(String[] args) {
// 1. Verify positive and negative integer powers
if (Math.pow(3.0, 4.0) == 81.0 && Math.pow(2.0, -2.0) == 0.25) {
System.out.println("Check 1 Passed: Integer exponents verified.");
}
// 2. Verify IEEE 754 zero-exponent rule
if (Math.pow(Double.NaN, 0.0) == 1.0 && Math.pow(0.0, 0.0) == 1.0) {
System.out.println("Check 2 Passed: Zero exponent always yields 1.0.");
}
// 3. Verify negative base with fractional exponent returns NaN
if (Double.isNaN(Math.pow(-4.0, 0.5))) {
System.out.println("Check 3 Passed: Negative fractional power returns NaN.");
}
System.out.println("All Math.pow() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Integer exponents verified.
Check 2 Passed: Zero exponent always yields 1.0.
Check 3 Passed: Negative fractional power returns NaN.
All Math.pow() verification checks passed successfully!
The first argument a is the base, and the second argument b is the exponent (computing a raised to the power b).
Java follows the IEEE 754 floating-point specification, which defines x^0 = 1.0 for all values of x, including 0.0 and NaN.
Because 1.0 / 3.0 cannot be represented as an exact odd integer ratio in binary floating-point, and negative bases with non-integer exponents return NaN. Use Math.cbrt(-8.0) to get -2.0.
Cast or round the returned double: int result = (int) Math.round(Math.pow(a, b)); via Math.round().
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.