When it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent

For example:

22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)

For larger exponents (over 31) use long instead:

232 = 1L << 32 = (long) Math.pow(2, 32)

BTW, in Kotlin you have shl instead of <<:

(Java) 1L << 32 = 1L shl 32 (Kotlin)

Answer from Stanislaw Baranski on Stack Overflow
🌐
Vultr Docs
docs.vultr.com › java › examples › calculate-the-power-of-a-number
Java Program to Calculate the Power of a Number | Vultr Docs
December 3, 2024 - In this article, you will learn how to implement power calculations in Java through examples. You'll explore different methods such as using a loop, the Math.pow() function, and recursion.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-calculate-power-of-a-number
Java Program to Calculate Power of a Number - GeeksforGeeks
// Java program to find the power of a number // using Recursion class GFG { // Function to calculate N raised to the power P static int power(int N, int P) { if (P == 0) return 1; else return N * power(N, P - 1); } // Driver code public static ...
Published   July 12, 2025
People also ask

Why not just use Math.pow() in Java?
Using Math.pow() is faster and easier, but writing your own logic with loops or recursion helps you understand how power calculation works step by step.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › power-number
Java Program to Calculate the Power of a Number (7 Ways)
What if the base is 0 and the exponent is positive?
The result will always be 0, because multiplying zero by any number still gives zero. For example, 0⁵ = 0.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › power-number
Java Program to Calculate the Power of a Number (7 Ways)
Why do we take reciprocal for negative exponents?
A negative exponent means division. For example, 2⁻³ = 1 / (2 × 2 × 2) = 1/8. That’s why we calculate normally, then take the reciprocal.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › power-number
Java Program to Calculate the Power of a Number (7 Ways)
🌐
Programiz
programiz.com › java-programming › examples › power-number
Java Program to Calculate the Power of a Number
In this program, we use Java's Math.pow() function to calculate the power of the given base.
🌐
Medium
rameshfadatare.medium.com › java-program-to-calculate-the-power-of-a-number-4e8f493e1c24
Java Program to Calculate the Power of a Number | by Ramesh Fadatare | Medium
December 13, 2024 - Calculate the Power: Implement logic to calculate the power using either a loop or the Math.pow() method. Display the Result: Print the result of the base raised to the power of the exponent.
Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › java › programs › power-number
Java Program to Calculate the Power of a Number (7 Ways)
October 19, 2025 - Explore 7 different Java programs to calculate the power of a number. Learn methods using for loops, recursion, Math.pow(), and more. Ideal for learners!
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › pow
Java Math pow() - Calculate Power | Vultr Docs
December 3, 2024 - Explore its basic usage, understand ... complex mathematical problems in Java. Define the base and the exponent. Call Math.pow(base, exponent) to compute the power....
🌐
Delft Stack
delftstack.com › home › howto › java › java power of integer
How to Calculate Power of Integers in Java | Delft Stack
February 2, 2024 - The number raised to the power of some other number can be calculated using this function. This method takes two parameters; the first number is the base while the second is the exponent.
🌐
Learn Java
javatutoring.com › power-of-a-number-java-program
Java Program To Calculate Power Of Number | 4 Ways
March 25, 2025 - Java Program To Calculate Power Of Number – In this article, we will detail in on the several ways to calculate the power of a number in Java programming.
🌐
TutorialsPoint
tutorialspoint.com › Java-program-to-calculate-the-power-of-a-number
Java program to calculate the power of a number
June 4, 2022 - import java.util.Scanner; public class PowerOfNumber { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the base number ::"); int base = sc.nextInt(); int temp = base; System.out.println("Enter the exponent number ::"); int exp = sc.nextInt(); for (int i=1; i<exp; i++){ temp = temp*temp; } System.out.println("Result of "+base+" power "+exp+" is "+temp); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - // Java program to demonstrate working // of Math.pow() method // importing java.lang package import java.lang.Math; class Geeks { // driver code public static void main(String args[]) { double a = 30; double b = 2; System.out.println(Math.pow(a, ...
🌐
Carmatec
carmatec.com › home › java math.pow() explained: the java power function
Java Math.pow() Explained: The Java Power Function
January 7, 2026 - Finance: Compound interest A = P(1 + r)^n, future value calculations. Physics: Exponential decay, gravitational or electromagnetic force laws. Computer Graphics: Scaling transformations, easing functions in animations. Statistics & Machine Learning: Normalization, activation functions, probability densities. Signal Processing: Frequency analysis involving powers. ... java double principal = 10000.0; double annualRate = 0.06; int years = 20; double futureValue = principal * Math.pow(1 + annualRate, years); System.out.printf("Future value: %.2f%n", futureValue);
🌐
BeginnersBook
beginnersbook.com › 2019 › 09 › java-program-to-calculate-power-of-a-number
Java Program to Calculate Power of a Number
September 12, 2019 - In the following program, we are using pow() function to calculate power. public class JavaExample { public static void main(String[] args) { int number = 10, p = 3; double result = Math.pow(number, p); System.out.println(number+"^"+p+" = "+result); } }
🌐
Educative
educative.io › answers › how-to-use-the-mathpow-method-in-java
How to use the Math.pow() method in Java
The Math.pow() is an built-in method in Java Math class and is used to calculate the power of a given number.
🌐
Just Academy
justacademy.co › blog-detail › how-to-calculate-power-in-java
How to calculate power in Java
March 10, 2025 - In Java, the Math.pow() method can be used to calculate the power of a number. This method takes two arguments - the base number and the exponent, and returns the result of raising the base to the power of the exponent.
🌐
GeeksforGeeks
geeksforgeeks.org › java › calculating-the-power-of-a-number-in-java-without-using-math-pow-method
Calculating the Power of a Number in Java Without Using Math pow() Method - GeeksforGeeks
January 22, 2026 - class GFG { static int power(int ... main(String[] args) { int base = 3, exponent = 3; System.out.println(base + "^" + exponent + " = " + power(base, exponent)); } } ... If exponent is 0, return 1 (base case)....
🌐
iO Flood
ioflood.com › blog › math-pow-java
Java's Math.pow() Function | Guide to Exponents in Java
July 16, 2024 - Think of Java’s Math.pow() function as a powerful calculator – allowing us to perform complex mathematical operations with ease, providing a versatile and handy tool for various tasks.
🌐
Studytonight
studytonight.com › java-programs › java-program-to-calculate-the-power-of-a-number
Java Program to Calculate the Power of a Number - Studytonight
February 29, 2024 - Enter the base value: 2 Enter the exponent value: 3 2 raised to the power 3 is: 8 · In this program, we will see how to calculate the power of a number using a for loop.