Java Math: Power

When we say raise a number to a power, we are talking about the exponent of a number. This is the amount of times a number is used in a multiplication. For example to raise 8 to the power of 2 we would write 82. This means that we would use 8 two times in a multiplication, or:
82 = 8 × 8 = 64
You might refer to 82 as 8 to the power 2, or 8 to the second power, or 8 squared.
Java has a Math method to calculate this for you, the pow() method and the general syntax (where x is the base and y is the exponent) is:
Math.pow(x,y)
Let’s see how we might use the pow() method in Java:
//Output 8 to the power of 2 System.out.println(Math.pow(8,2)); //Output 8 to the power of 3 System.out.println(Math.pow(8,3)); //Output 8 to the power of 4 as an integer System.out.println((int)Math.pow(8,4));
The output should be 82, 83 and 84:
run: 64.0 512.0 4096 BUILD SUCCESSFUL (total time: 0 seconds)
Note that pow() will return a double value, so if you would like to remove the decimal point you can type cast the result to an integer.