Math pow() Method in Java with Example
Last Updated : 28 Mar, 2025
The Math.pow() method in Java is used to calculate a number raised to the power of some other number. It is part of the java.lang.Math class and is widely used in mathematical computations, scientific calculations, and algorithmic problems. This function accepts two parameters and returns the value of first parameter raised to the second parameter.
Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).
Java // Java program to demonstrate working // of Math.pow() method // importing java.lang package import java.lang.Math; class Geeks { // driver code public static void main(String args[]) { double a = 30; double b = 2; System.out.println(Math.pow(a, b)); a = 3; b = 4; System.out.println(Math.pow(a, b)); a = 2.5; b = 6.9; System.out.println(Math.pow(a, b)); } } Output900.0 81.0 556.9113382296638
Explanation: In the above code, we have declared two variables a (base) and b (exponent) and then the program calculates the result of raising the base (a) to the power of the exponent (b) using the Math.pow() method and prints the result.
Syntax of Math.pow() Method
The method signature is listed below:
public static double pow(double base, double exponent)
Parameter:
- base (double): The number to be raised (e.g., 2 in 2³)
- exponent (double): The power to raise the base (e.g., 3 in 2³).
Return Types: This method return a floating-point number (e.g., 8.0, 2.5, NaN).
Key features and Special Cases
The below table demonstrates the special cases:
Cases | Result |
|---|
exponent = 0 | 1.0 (any number⁰ = 1) |
|---|
exponent = 1 | Same as base (e.g., 5¹ = 5) |
|---|
exponent = NaN | NaN (Not a Number) |
|---|
base = 0, exponent < 0 | Infinity |
|---|
base = NaN | NaN |
|---|
Important Points:
- Any number raised to the power of 0 equals 1. This is a fundamental rule in mathematics, known as the "zero exponent rule." For example, 5⁰ = 1, 10⁰ = 1, etc.
- Any number raised to the power of 1 is the number itself. This is because raising a number to the power of 1 does not change its value. For example, 5¹ = 5, 10¹ = 10, etc.
- If the exponent is "NaN" (Not a Number), the result of the power operation becomes "NaN" as well.
- When the base is 0 and the exponent is a negative number (e.g., 0 raised to a negative power), it results in infinity.
- If the base is "NaN" (Not a Number), any power of it also results in "NaN."
Example 2: This example demonstrates how the Math.pow() method handles special cases, such as NaN, 0, and 1 as exponents.
Java // handling different cases Nan, 0 and 1 as exponents. // importing java.lang package import java.lang.Math; public class GFG { public static void main(String[] args) { double nan = Double.NaN; double result; // Here second argument is NaN, // output will be NaN result = Math.pow(2, nan); System.out.println(result); // Here second argument is zero result = Math.pow(1254, 0); System.out.println(result); // Here second argument is one result = Math.pow(5, 1); System.out.println(result); } } Explanation: The above code handles special cases in exponentiation such as When the exponent is NaN, the result is NaN, When the exponent is 0, the result is 1.0 (except for base 0), When the exponent is 1, the result is the base itself.
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java
My Profile