Signature of Java Math pow()
public static double pow(double base, double exponent)

You can also try this code with Online Java Compiler
Run CodeSyntax of Math pow() function in Java
Math.pow(a,b)

You can also try this code with Online Java Compiler
Run CodeThe syntax Math.pow(a, b) refers to a function where a and b are arguments. a is the base number, and b is the exponent. The function calculates and returns the value of a raised to the power of b. For example, if a is 2 and b is 3, it calculates
Explanation
The above function takes two arguments, usually of double data type. The above function returns base^exponent as output. Here a is the base, and b is the exponent. Its time complexity is O(log b), and space complexity is O(1).
Also see, Swap Function in Java
How Do I Use the Math Pow() Function in Java?
In Java, the power function is used through `Math.pow(double a, double b)`, where `a` is the base and `b` is the exponent. It returns `a` raised to the power of `b`. For example, `Math.pow(2, 3)` returns 8.0, as it computes (23).
Example of Math Pow() function in Java
In the below example, we have shown some different combinations of base and exponents for better understanding.
public class NinjaPower {
public static void main(String[] args) {
// Case 1
double base = 2;
double exponent = 3;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
// Case 2
base = 2;
exponent = 0.5;
result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
// Case 3
base = 0.2;
exponent = 3;
result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
2.0 to the power of 3.0 is 8.0
2.0 to the power of 0.5 is 1.4142135623730951
0.2 to the power of 3.0 is 0.008000000000000002
Some Special Cases of Power Function in Java
There are some peculiar cases when the argument has different values. Let’s discuss them:
Second Argument as Zero
If the second argument is a positive or negative zero, then the power function returns 1.0.
Code
class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = -0;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
5.0 to the power of 0.0 is 1.0
Second Argument as NaN
If the second argument is not a number (NaN), then the power function also returns NaN.
Code
class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = Double.NaN;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
5.0 to the power of NaN is NaN
Second Argument as Negative
If the second argument is a negative number, then the power function solves it just like general maths, i.e., a^(-x) becomes (1/a)^x.
Code
class CodingNinja {
public static void main(String[] args) {
double base = 5.0;
double exponent = -2;
double result = Math.pow(base, exponent);
System.out.println(base + " to the power of " + exponent + " is " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
5.0 to the power of -2.0 is 0.04
How to Return Integer type Value Using the Power Function?
To return an integer value using the power function in Java, first use `Math.pow()` to compute the power, which returns a double, and then cast the result to an integer. For example, `(int) Math.pow(2, 3)` calculates (23) as 8.0 and then casts it to 8, an integer.
Example :
public class Main {
public static void main(String[] args) {
int base = 2;
int exponent = 3;
int result = (int) Math.pow(base, exponent); // Calculates 2^3 and casts the result to an integer
System.out.println("Result: " + result); // Outputs: Result: 8
}
}

You can also try this code with Online Java Compiler
Run CodeIn this code, Math.pow(2, 3) computes 2323 and returns 8.0 as a double. The (int) before Math.pow() casts this double to an integer, making result equal to 8.
Frequently Asked Questions
How do you write 2 power n in Java?
In Java, to write 2 raised to the power of n, you use `Math.pow(2, n)`. This function takes 2 as the base and n as the exponent, computing and returning (2n). For example, `Math.pow(2, 3)` returns 8.0.
What does Math POW () return?
The `Math.pow()` function in Java returns a double value representing the result of raising the first argument (base) to the power of the second argument (exponent). For example, `Math.pow(2, 3)` returns 8.0, which is (23).
What is Math pow with 3 arguments?
The Python `pow()` function with three arguments, `pow(base, exponent, modulus)`, computes the power and then applies the modulus, returning the remainder of the power divided by the modulus. This is useful for modular exponentiation, especially in cryptography.
Is there any alternative to the power function in Java?
We can use the exponentiation operator (**) or implement the power function using a loop. The Math.pow() method is generally the simplest and most efficient way to perform power functions in Java.
Conclusion
In this article, we discussed the Power function in Java. We also saw different examples and some peculiar cases and their output. The power function is provided in almost all major programming languages, but they should be coded correctly, and the edge cases should be kept in mind.
If you liked this, read our other related blogs: