Power function in java

Table of Contents

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.

Related Posts

  • 28 September

    PI in Java

    Table of ContentsPI Constant in JavaPI Constant in Java Math Class The Pi is a constant value in Mathematics that represents a floating-point value 3.1415. It is mostly used in geometry to calculate area, circumference, volume, etc. If you have studied geometry in your academic, then you may be aware of use of Pi such […]

  • 05 October

    Calculate total surface area of Cylinder in java

    In this post, we will see how to calculate total surface area of Cylinder in java. Formula of calculating total surface area of Cylinder is: Surface area of Cylinder = 2 *Π * r * r + 2 *Π * r * h Where r is radius of cylinder and h is height of cylinder […]

  • 05 October

    Calculate total surface area of Hemisphere in java

    In this post, we will see how to calculate total surface area of Hemisphere in java. Hemisphere is exactly half of sphere. There can be many practical examples of Hemisphere. You can divide earth into two hemisphere i.e. Northen Hemisphere and Southern Hemisphere. Formula of calculating total surface area of Hemisphere is: surface area of […]

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-62735085196f5811328614/] When you run above program, […]

  • 03 January

    java round double/float to 2 decimal places

    Table of ContentsUsing DecimalFormatRead also:7 ways to format double to 2 decimal places in java7 ways to format float to 2 decimal places in javaUsing BigDecimalUsing Math.round(double*100.0)/100.0Using Apache common Math In this post, we will see how to round double to 2 decimal places in java. There are many ways to do it.Let’s go through […]

  • 03 January

    How to get square root of number in java

    Table of ContentsSyntaxReturn typeSquare root of number In this tutorial. we will see if how to get square root of number in java. It is very simple to get square root of number in java. You can simply use Math’s sqrt() method to calculate square root of number. Syntax [crayon-6273508519e6e526553190/] Return type It returns square […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.