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
Discussions

Why there is no power operator in Java / C++? - Software Engineering Stack Exchange
It is easy to make one for classes you define in C++ with operator overloading (and I believe such thing is possible also in Java), but when talking about primitive types such as int, double and so on, you'll have to use library function like Math.power (and usually have to cast both to double). More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 3, 2011
java - Multiplication vs ^ operator vs Math.pow() for integer powers - Stack Overflow
The ^ operator is not performing exponentiation - it's a bitwise "exclusive OR" (aka "xor"). Using integer math for 100000000 raised to the fourth power will give incorrect results - a 32-bit integer cannot store numbers that large. More on stackoverflow.com
🌐 stackoverflow.com
Can somebody please help me with Math.pow and specifically raising to the power of.
This is what I have at the minute but it doesn't work. In what way doesn't it work? Please let us know the actual error you're getting. For your pasted code, Java syntax is much more rigid than math syntax. See here for a reference on Java operators. In particular, in math notation, multiplication is implied when you put two things next to each other. That's not true of Java. You need to explicitly use the multiplication operator. EDIT: It also seems you need to define k and ans unless they're instance variables with definitions you haven't included with your code snippet. If you haven't programmed in Java I'd strongly recommend reading some reference material. There are docs on the official Oracle site here More on reddit.com
🌐 r/java
6
0
November 26, 2013
Do I round up or down in java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
20
14
May 12, 2023
🌐
W3Schools
w3schools.com › java › ref_math_pow.asp
Java Math pow() Method
Operators Arithmetic Assignment Comparison Logical Precedence Code Challenge Java Strings · Strings Concatenation Numbers and Strings Special Characters Code Challenge Java Math Java Booleans
🌐
Programiz
programiz.com › java-programming › examples › power-number
Java Program to Calculate the Power of a Number
class Main { public static void main(String[] args) { // negative number int base = -3, exponent = 2; double result = Math.pow(base, exponent); System.out.println("Answer = " + result); } } Output · Answer = 9.0 · Here, we have used the pow() method to compute the power of a negative number ...
🌐
EyeHunts
tutorial.eyehunts.com › home › math pow java | power ^ operator function | example
Math Pow Java | Power ^ Operator Function | Example - EyeHunts
August 6, 2021 - Answer: As an above answer we can do it with 3 ways using java loops or java pow function. Let see an example with pow() methods. “Given an integer, write a function to determine if it is a power of two.”
🌐
Delft Stack
delftstack.com › home › howto › java › java power of integer
How to Calculate Power of Integers in Java | Delft Stack
February 2, 2024 - First, if the number is greater than 0 but the power is 0 it will return 1. If the power is not 0, but the number is 0, then 0 is returned. In the other case, we run a loop that calculates the given number’s exponent raised to the given power.
🌐
GeeksforGeeks
geeksforgeeks.org › java › math-pow-method-in-java-with-example
Math pow() Method in Java with Example - GeeksforGeeks
March 28, 2025 - Example 2: This example demonstrates how the Math.pow() method handles special cases, such as NaN, 0, and 1 as exponents. ... // handling different cases Nan, 0 and 1 as exponents.
Find elsewhere
🌐
Quora
quora.com › How-do-we-use-the-pow-function-in-Java
How do we use the pow() function in Java? - Quora
You can use math pow() function by using this syntax.pow(a,b) in C , first you have to use #include<math.h>.In Java you can use by importing utils methods and use by this syntax math.pow(a,b) These a and b are the parameters the a is the number ...
🌐
Educative
educative.io › answers › how-to-use-the-mathpow-method-in-java
How to use the Math.pow() method in Java
The power of a number refers to how many times to multiply the number with itself. ... The method takes the base and exponent parameters of type double. The method calculates multiplication of the base with itself exponent times and returns the result of type double. ... The pow() method is a part of java.lang.Math class, you need to import this class in your code to use the function.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › pow
Java Math pow() - Calculate Power | Vultr Docs
December 3, 2024 - int intBase = 5; int intExponent = 2; double result = Math.pow(intBase, intExponent); System.out.println("5 raised to the power of 2 is: " + result); Explain Code · In this example, even though Math.pow() expects double arguments, Java's automatic type conversion allows integers to be used directly.
🌐
Xah Lee
xahlee.info › java-a-day › power_operator.html
Java: The Power Function
Java doesn't provide the power operator. (e.g. “^” in 3^4). You have to use java.lang.Math.pow(3,4). That method returns type “double”. import java.lang.Math; class T2 { public double square (int n) { return java.lang.Math.pow(n,2); } } class T1 { public static void main(String[] arg) ...
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++).

🌐
CodeGym
codegym.cc › java blog › java math › math pow() method in java
Math.pow() Method in Java
December 5, 2024 - Therefore, instead of writing a ... java.lang package as a method of the Math library. It is used to calculate the power of numbers, both integers as well as doubles....
🌐
Codecademy
codecademy.com › docs › java › math methods › .pow()
Java | Math Methods | .pow() | Codecademy
June 23, 2025 - It returns NaN when the result ... have a built-in exponentiation operator (^ is the XOR operator). Math.pow() is the standard way to perform exponentiation in Java, providing accurate results for both integer and ...
🌐
Octoperf
octoperf.com › blog › 2018 › 03 › 16 › java-math-pow
Java Math.pow Through Code Examples - OctoPerf
March 16, 2018 - package com.octoperf; import org.junit.Test; import static java.lang.Double.NEGATIVE_INFINITY; import static java.lang.Double.NaN; import static java.lang.Double.POSITIVE_INFINITY; import static org.junit.Assert.assertEquals; public class MathPowTest { private static final double DELTA = 0.001d; @Test public void simpleExample() { assertEquals(1024d, Math.pow(2d, 10d), DELTA); } } The 10th power of 2 is 1024. The result is a double. Beware of casting the result to: an integer: it could overflow, which means the double value does not fit into the int value.
🌐
iO Flood
ioflood.com › blog › math-pow-java
Java's Math.pow() Function | Guide to Exponents in Java
February 29, 2024 - While Math.pow() is a powerful tool for exponentiation, it’s not the only way to perform this operation in Java. Let’s explore some alternative approaches, such as using loops or the BigInteger class. One straightforward method to calculate the power of a number is to use a loop. Here’s an example: int base = 2; int exponent = 3; int result = 1; for(int i = 0; i < exponent; i++) { result *= base; } System.out.println(result); // Output: // 8
🌐
Octoperf
blog.octoperf.com › java-mathpow-through-code-examples
Java Math.pow Through Code Examples - Load Testing Blog
March 16, 2018 - package com.octoperf; import org.junit.Test; import static java.lang.Double.NEGATIVE_INFINITY; import static java.lang.Double.NaN; import static java.lang.Double.POSITIVE_INFINITY; import static org.junit.Assert.assertEquals; public class MathPowTest { private static final double DELTA = 0.001d; @Test public void simpleExample() { assertEquals(1024d, Math.pow(2d, 10d), DELTA); } } The 10th power of 2 is 1024. The result is a double. Beware of casting the result to: an integer: it could overflow, which means the double value does not fit into the int value.
🌐
Codingzap
codingzap.com › home › blog – programming & coding articles › what is java exponent? how to do exponents in java?
What is Java Exponent? How to do exponents in Java? - Codingzap
September 25, 2025 - What is Java Exponent? Learn how to do exponents in java using loop. How to use pow() method? Code and Examples explained.