Power function in java

Previous
Next

Power function in java is used to get first argument’s power to 2nd argument.
For example:
Let’s say you want to calculate 2 to the power 4.

That will be 2*2*2*2=16
In java, you can do it by :
Math.pow(2,4) =16

Syntax

Example :
Let’s use power function in java.

When you run above program, you will get below output:

Math.pow(2 ,4)=16.0
Math.pow(4.2,3)=74.08800000000001

If you want to return integer from power function, you need to explicitly cast it to Integer.

When you run above program, you will get below output:

Math.pow(2 ,4)=16

Internal working

Math.pow is native method in java and it is written in c.
Let me provide you source code for Math.pow method.

Math.java

So if you above code, Math.pow delegate call to StrictMath.
StrictMath.java

As you can see it calls native method.

jdk/src/share/native/java/lang/fdlibm/src/w_pow.c

jdk/src/share/native/java/lang/fdlibm/src/e_pow.c

That’s all about power funcation in java.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog