# Java Exponent | Power Operator | Examples

Java exponent method math pow can do calculate exponent. The math pow method is not the only way to do it, With your own code logic, you can do number exponent.

You use the Math.pow(a, b) to calculate a^b. Where “b” is Exponent and it says how many times to use the “a” number in a multiplication.

Exponents are also called Powers or Indices.

Note: There is no Exponent operator, but there is a method in the Math java module.

## Java exponent operator Example

Simple use method Math.pow(double a, double b). Which returns the double type value of the first argument raised to the power of the second argument. See below example of how to do it.

```import java.lang.Math;

public class Hello {

public static void main(String args[])
{
double result = 0.0;
result = Math.pow(8,3);

System.out.println("Exponent Number is: " + result);
}
}
```

Output: Exponent Number is: 512.0

### Java Exponent Integer | Numbers

To get the integer value you have to do int casting. See below example of it.

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

Follow this tutorial for casting in java- Type Casting in Java

```import java.lang.Math;

public class Hello {

public static void main(String args[])
{
int result = 0;
result = (int)(Math.pow(8,3));

System.out.println("Exponent Number is: " + result);
}
}
```

Output: Exponent Number is: 512

### Own Formula | not recommended for big power

For example, instead of writing Math.pow(x, 2) write x * x or Math.pow(x, 3), just write x * x * x.

For same upper example- result = 9*9*9*9*9;

It’s not a good idea if the exponent value is very large. For example, the exponent value is 1000. Then you can’t write it 1000 times multiple numbers.

### Let’s see other ways- For-loop statement

First, create a methodmyPow” with 2 arguments one is for base value double and another value exponent integer. And the return value is also double type.

```public class Hello {

public static void main(String args[]) {

double base = 2;
int exp = 3;
double result = myPow(2, 3);

System.out.println(result);
}

// Works only for b >= 1
public static double myPow(double a, int b) {
double res = 1;
for (int i = 0; i < b; i++) {
res *= a;
}
return res;
}
}
```

Output: 8.0

### Try Java math pow negative exponent

It will work fine with some rules- if the exponent is even then resulted is positive and if the exponent is odd then the result is a negative value.

```import java.lang.Math;

public class Hello {

public static void main(String args[])
{

int resultN = (int)(Math.pow(-8,3)); // odd
int resultP = (int)(Math.pow(-8,2)); // Even

System.out.println("Negative: " + resultN);
System.out.println("Positive: " + resultP);
}
}
```

Output: Negative: -512
Positive: 64

### Q: How to get exponents without using the math.pow for java

Answer: By using a for loop you can do it. See below code for it.

```public static double myPow(double a, int b) {
double res = 1;
for (int i = 0; i < b; i++) {
res *= a;
}
return res;
}
```

Follow this link for more examples (java power operator)- Math Pow Java | Power ^ Operator Function

Do comment if you have any doubts and suggestions.

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  Java exponent operator﻿ examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

## 1 thought on “Java Exponent | Power Operator | Examples”

1. Any one can solve this:
Create a class name Exponents and Write a function power(base , exponent) that takes 2 parameters of type ” int” that returns the value of base exponent. Base is nonzero positive integer while exponent is positive integer.

Call this power method within function button_click of windows form class that reads integer values for base and exponent from user and performs the calculation written in the power method of Exponents class. If returned value is even it should display on label otherwise display it on textbox. Built-in functions are not allowed to use.(Assume label name lblDisplay and textbox name txtDisplay)

This site uses Akismet to reduce spam. Learn how your comment data is processed.