Skip to content

Math Pow Java | Power ^ Operator Function | Example

  • by

A Math pow Java method is used to calculate a number raised to the power of some other number. To use this method you need to import java.lang.Math package. A Full form of the java pow method in the java power function.

pow = power = ^ (Power operator)

Note: ^ in java does not mean to raise to a power. It’s means XOR.

Math Pow Java Power Operator Function Example

In this tutorial, you will learn about the basics of Java math power function with some important interview questions.

Syntax | Method’s signature | Java ^ operator

A method signature. It returns a double value and takes input values.

public double pow(double a, double b)

For integer(int)

int intResult = (int) Math.pow(7, 3);

Note: the int casting is required if you want to have an Integer result. Follow this tutorial for casting in java- Type Casting in Java 

For double value

double dblResult = Math.pow(7.9, 3);


  • a: this parameter is the base
  • b: this parameter is the exponent.

Return :

A Math.pow(a, b) method returns a^b.

A Simple | Math pow java example

Use the pow method with Math class and pass the values. See below example code of math pow java int.

public class Hello {

    public static void main(String arg[]) {
        double a = 10;
        double b = 2;
        System.out.println(Math.pow(a, b)); // 10 ^ 2

        b = 3;
        System.out.println(Math.pow(a, b)); // 10 ^ 3

        b = 4;
        System.out.println(Math.pow(a, b)); // 10 ^ 4


Output: You can see the output calculation 10 ^ 2 = 10 * 10 = 100, Then other one 10 ^ 3 = 10 * 10 * 10 = 1000 etc.


Interview Questions on java pow function/operator

Q: How to calculate the power of a number in java?

Answer: To calculate the power of a number in java you can do it in many ways.

Q: How to java power of 2?

Answer: As an above answer we can do it with 3 ways using java loops or java pow function. Let see an example with pow() methods.

“Given an integer, write a function to determine if it is a power of two.”

public boolean isPowerOfTwo(int n) {
    return n>0 && n==Math.pow(2, Math.round(Math.log(n)/Math.log(2)));

Q: How to implement a power function in java?

Answer: you can implement your own function of use math pow like below.

int intResult = (int) Math.pow(7, 3);

OR create on power function example

public class Hello {

    /* Function to calculate xa raised to the power y */
    static int power(int x, int y)
        if (y == 0)
            return 1;
        else if (y % 2 == 0)
            return power(x, y / 2) * power(x, y / 2);
            return x * power(x, y / 2) * power(x, y / 2);

    /* Program to test function power */
    public static void main(String[] args)
        int x = 3;
        int y = 3;

        System.out.printf("%d", power(x, y));

Output: 27

If you have any doubts and questions, then do comment below. Give suggestions and examples will help others.

Follow this tutorial “Java operators” to learn about other basic operators in java.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All  Math Pow Java examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *