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
🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - Example 1: This example demonstrates how to use the Math.pow() method in Java to calculate the power of a number (base raised to the exponent).
Discussions

Why there is no power operator in Java / C++? - Software Engineering Stack Exchange
While there is such operator - ** in Python, I was wondering why Java and C++ don't have one too. It is easy to make one for classes you define in C++ with operator overloading (and I believe such... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 3, 2011
How does Java calculate Math.pow(a,b) and how do I figure out how to figure out how it does it?
From the source code: if (x != x || y != y) WTF?? More on reddit.com
🌐 r/javahelp
18
9
June 10, 2015
Raising a number to a power in Java - Stack Overflow
Here is my code. For some reason my BMI is not calculated correctly. When I check the output on a calculator for this : (10/((10/100)^2))) I get 1000, but in my program, I get 5. I'm not sure wha... More on stackoverflow.com
🌐 stackoverflow.com
How to output a number raised to the power in Java
Daniel Silva is having issues with: Hello, I'm finishing up this application I am writing that displaced the cubic feet of boxes. However it's just printing 355ft. I need it to pri... More on teamtreehouse.com
🌐 teamtreehouse.com
3
January 16, 2017
🌐
Codecademy
codecademy.com › docs › java › math methods › .pow()
Java | Math Methods | .pow() | Codecademy
June 23, 2025 - The Math.pow() method in Java is a static method that calculates the value of a base number raised to the power of an exponent. It performs exponentiation operations by taking two double parameters and returning the result as a double value.
🌐
W3Schools
w3schools.com › java › ref_math_pow.asp
Java Math pow() Method
System.out.println(Math.pow(2, ...ln(Math.pow(8, -1)); System.out.println(Math.pow(10, -2)); ... The pow() method raises a number to the power of another number....
Top answer
1 of 9
34

Generally speaking, the primitive operators in C (and by extension C++) are designed to be implementable by simple hardware in roughly a single instruction. Something like exponentiation often requires software support; so it's not there by default.

Also, it's provided by the standard library of the language in the form of std::pow.

Finally, doing this for integer datatypes wouldn't make much sense, because most even small values for exponentiation blow out the range required for int, that is up to 65,535. Sure, you could do this for doubles and floats but not ints, but why make the language inconsistent for a rarely used feature?

2 of 9
45

This question is answerable for C++: Stroustrup, "Design and Evolution of C++" discusses this in section 11.6.1, pp. 247-250.

There were general objections to adding a new operator. It would add to the already overcomplicated precedence table. The members of the working group thought it would give only minor convenience over having a function, and they wanted to be able to substitute their own functions sometimes.

There was no good candidate for an operator. ^ is exclusive-or, and ^^ invited confusion because of the relationship between & and | and && and ||. ! was unsuitable since there would be the natural tendency to write != for exponentiation of an existing value, and that was already taken. The best available may have been *^, which apparently nobody really liked.

Stroustrup considered ** again, but it already has a meaning in C: a**p is a times whatever p points to, and char ** c; declares c as a pointer to pointer to char. Introducing ** as a token meaning "declaration of a pointer to pointer to", "times what the next thing points to" (if it's a pointer) or "exponentiation" (if followed by a number) caused precedence problems. a/b**p would have to parse as a/(b**p) if p were a number, but (a/b) * *p if p were a pointer, so this would have to be resolved in the parser.

In other words, it would have been possible, but it would have complicated the precedence table and the parser, and both are already too complicated.

I don't know the story about Java; all I could do would be speculate. As for C, where it started, all C operators are easily translated into assembly code, partly to simplify the compiler and partly to avoid hiding time-consuming functionality in simple operators (the fact that operator+() and others could hide great complexity and performance hits was one of the early complaints about C++).

🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › pow
Java Math pow() - Calculate Power | Vultr Docs
December 3, 2024 - This code snippet computes (2^3), resulting in 8.0. Here, Math.pow() takes two double arguments and returns the base raised to the power of the exponent as a double. Use integer values that Java implicitly converts to double.
Find elsewhere
🌐
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.
🌐
Edureka
edureka.co › blog › java-power-function
What is Power Function in Java | Java pow() Method Example | Edureka
June 17, 2021 - ... Power function in Java is used to calculate a number raised to the power of some other number. This function accepts two parameters and returns the value of the first parameter raised to the second parameter.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
1 week ago - scaleFactor - power of 2 used to scale d · Returns: d × 2scaleFactor · Since: 1.6 · public static float scalb(float f, int scaleFactor) Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set. See the Java Language Specification for a discussion of floating-point value sets.
🌐
Carmatec
carmatec.com › home › java math.pow() explained: the java power function
Java Math.pow() Explained: The Java Power Function
January 7, 2026 - No import statement is required because Math belongs to the java.lang package, which is automatically imported. The most common use case is raising a number to a positive integer power:
🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - Math.pow is a method in Java, provided by java.lang.Math to calculate the power of different numbers
🌐
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
medium.com › @AlexanderObregon › javas-math-pow-method-explained-7c0f746ad420
Understanding Java's Math.pow() Method | Medium
July 16, 2024 - Java’s Math.pow() method is a powerful tool for performing exponentiation, allowing developers to raise a number to a specified power.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
What is the correct way to raise a number to a power in Java? - LambdaTest Community
December 11, 2024 - I’m trying to calculate BMI in my Java program, but the output seems incorrect. When I check the result with a calculator for this formula: (10 / ((10 / 100) ^ 2)) I get 1000, but in my program, I get 5. Here’s my code: bmi = (weight / ((height / 100) ^ 2)); // Incorrect calculation It looks like the exponentiation (^2) isn’t working as expected.
🌐
CodeAhoy
codeahoy.com › java › Math-Pow-method-JI_11
Java Math.pow() Method with Examples | CodeAhoy
October 26, 2016 - The Math.pow(a, b) is a built-in method of the Math class that’s used for calculating the power of a given number.