Javatpoint Logo
Javatpoint Logo

Power Function in Java

The power function in Java is Math.pow(). It is used to get the power of the first argument to the second argument. It takes two arguments and returns the value of the first argument raised to the second argument. It returns a double type value.

The pow() function takes place in java.lang.Math.pow () library.

For example, to calculate the 5 to power 2, then it can be done as follows:

Math.pow(5,2) =25

Syntax:

where,

a is base

b is exponent

The above syntax will return the value of a^b in double data type.

Consider the below points about the power function in Java:

  • It will return 1.0, if the second argument is positive or negative zero.
  • It will return NaN, if the second argument is not a number.
  • It will return the same result as the first argument, if the b (second argument) is 1.

Let's understand it with some examples:

Example1: Calculate 5^2 using Java Power function

PowerFunc1.java:

Output:

25.0

Example2: Calculate 5^(-3) using power function

PowerFunc2.java:

Output:

0.008

Example3: Calculate -3^5 using the power function

PowerFunc3.java:

Output:

-243.0

Example4: calculate 5^0 using the power function

PowerFunc4.java:

Output:

1.0

Example5: calculate 0.57^0.25 using the power function

PowerFunc5.java:

Output:

0.8688978326173192

From the above examples,

  • The power function is used to get the value of the first argument's power to the second argument.
  • The power function returns the double type value.
  • The negative number can be calculated using the power function.
  • The decimal number can also be calculated using the power function.
  • If the second argument is zero, the result will be 1.0.
  • If the second argument is not a number, it will return NaN.

How to Return Integer type Value Using the Power Function

We can also return the integer type value using the power function. For this, we need to explicitly cast it to Integer.

Consider the below example:

PowerFunc6.java:

Output:

25






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA