java.lang.Math pow(double a,double b)

Description :

This java tutorial shows how to use the pow(double a, double b) method of Math class under java.lang package. This method just return the result of raising a to the power of b.

Method Syntax :

public static double pow(double a,double b)

Parameter Input :

DataType Parameter Description
double a the base
double b the exponent

Method Returns :

The abs(double a) method simply returns the value of the first argument raised to the power of the second argument.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of pow(double a,double b) method of Math class. Basically we just get the result of raising the base to the exponent both comes from the user input.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of 
 * power(double a,double b) method of Math class
 * Get the result of raising the base number to exponent
 */

public class MathPower {

	public static Scanner scan;
	public static void main(String[] args) {
		// ask for user input
		out.print("Enter the base number:");
		scan = new Scanner(System.in);
		// use scanner to get user console input, base
		double baseNumber = scan.nextDouble();
		// use scanner to get user console input, exponent
		out.print("Enter the exponent:");
		scan = new Scanner(System.in);
		double exponent = scan.nextDouble();		
		// get the result
		double result = Math.pow(baseNumber, exponent);		
		out.println("Result = "+result);		
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

Sample Output :

Running the power(double a,double b) method example source code of Math class will give you the following output

Enter the base number:3
Enter the exponent:2
Result = 9.0

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :