The fastest way to square a number is to multiply it by itself.

Why is Math.pow so slow?

It's really not, but it is performing exponentiation instead of simple multiplication.

and why does it cope badly with > 1 and even worse with < -1 numbers

First, because it does the math. From the Javadoc it also contains tests for many corner cases. Finally, I would not rely too much on your micro-benchmark.

Answer from Elliott Frisch on Stack Overflow
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java numbers โ€บ how to square a number in java
How to Square a Number in Java
September 28, 2023 - Square of 3.5 is: 12.25 Square of 11.1 is: 123.21 Square of 13.0 is: 169.0 ยท In this example, we have used Math.pow(number, POW) method provided by Java to multiply the number with itself to the times โ€œPOWโ€ is passed.
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ how-square-number-in-java-method-function
Java: How to square a number | alvinalexander.com
January 31, 2026 - This is how you square a number by multiplying it by itself: ... In this example if you print the value of square, it will be 4.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-square-in-Java
How to square in Java - Quora
Java (programming languag... ... To square a number in Java you multiply it by itself.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_sqrt.asp
Java Math sqrt() Method
Java Wrapper Classes Java Generics ... Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove ...
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ checking-if-a-number-is-a-perfect-square-in-java-fce4d6fc80ec
Checking if a Number Is a Perfect Square in Java | Medium
October 21, 2025 - Before calling Math.sqrt was an option, one common way to check for a perfect square was to loop through integers, squaring each one, until either a match was found or the squared value passed the target. The logic is slower for large numbers but useful for teaching how iteration and conditions combine in Java.
๐ŸŒ
Study.com
study.com โ€บ business courses โ€บ business 104: information systems and computer applications
How to Square a Number in Java - Lesson | Study.com
March 14, 2018 - If you need to square a number, a very simple solution is to just multiply the number by itself. After all, 52 is really 5 * 5. In this instance, it's totally acceptable to multiply a number by itself, although there is another tool that will ...
Find elsewhere
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1566992 โ€บ java-program-to-calculate-square-of-a-numberneed-help-with-that-please
Java program to calculate square of a number......need help with that please | Sololearn: Learn to code for FREE!
Write a simple program to calculate the square of a number when it is imputed must accept multiples of 2 ... Yup Paul Dey I suppose that code won't work... ok first of all, ask yourself, what is the meaning of int x {2,6,8,10,12}... ... Ok that is not how arrays are defined in Java....check these links to clear things out : https://www.sololearn.com/learn/Java/2139/ https://www.sololearn.com/learn/Java/2148/ https://www.sololearn.com/learn/Java/2208/ https://www.sololearn.com/learn/Java/2147/
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ java tutorial โ€บ squares in java
Squares in Java | Examples of the Squares in Java
May 13, 2024 - In this program, we are going to see the sum of squares of the first N natural numbers. We enter the value of N, and the program calculates the sum of squares of the first N natural numbers. ... // Java Program to find sum of // square of first n natural numbers import java.io.*; class SumofSquares { // Return the sum of the square of first n natural numbers static int square sum(int n) { // Move the loop of I from 1 to n // Finding square and then adding it to 1 int sum = 0; for (int i = 1; i <= n; i++) sum += (i * i); return sum; } // Main() used to print the value of sum of squares public static void main(String args[]) throws IOException { int n = 6; System.out.println("The sum of squares where N value is 6 is "+ squaresum(n)); } }
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ calculate-square-of-a-number-without-using-and-pow
Calculate square of a number without using *, / and pow() - GeeksforGeeks
March 29, 2023 - // Simple solution to calculate square without // using * and pow() #include <iostream> using namespace std; int square(int n) { // handle negative input if (n < 0) n = -n; // Initialize result int res = n; // Add n to res n-1 times for (int i = 1; i < n; i++) res += n; return res; } // Driver code int main() { for (int n = 1; n <= 5; n++) cout << "n = " << n << ", n^2 = " << square(n) << endl; return 0; } ... // Java Simple solution to calculate // square without using * and pow() import java.io.*; class GFG { public static int square(int n) { // handle negative input if (n < 0) n = -n; // Initialize result int res = n; // Add n to res n-1 times for (int i = 1; i < n; i++) res += n; return res; } // Driver code public static void main(String[] args) { for (int n = 1; n <= 5; n++) System.out.println("n = " + n + ", n^2 = " + square(n)); } } // This code is contributed by sunnysingh
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-get-the-square-cube-and-square-root-of-a-number-in-java
How to get the square, cube, and square root of a number in Java
In Java, we can get the square, cube, and square root of a number using the Math class. With the Math.pow() method, we can get the cube and square of the number.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ program to find square and square root in java
Program to Find Square and Square Root in Java - Scaler Topics
April 25, 2024 - If you pass an integer or any primitive ... 2) = 100.0 ยท So, in order to find the square of a number, we can pass the base as the number and the exponent as 2....
๐ŸŒ
Medium
medium.com โ€บ edureka โ€บ java-sqrt-method-59354a700571
How to Calculate Square and Square Root in Java? | Edureka
September 18, 2020 - This article discusses about the different ways to find square and square root in Java.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ java-sqrt-method
Java Sqrt(): Program to Find Square and Square Root in Java
July 5, 2024 - This article on Java square and square root in java will help you learn how to calculate square and square root in java in multiple ways. Read this blog now
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ dsa with java โ€บ check for perfect square in java
Check for Perfect Square in Java | PrepInsta
March 10, 2023 - In this method we use the floor and ceil function. If they are equal that implies the number is a perfect square. ... import java.io.*; public class Main{ static void checkperfectsquare(int n) { if (Math.ceil((double)Math.sqrt(n)) == ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Math.html
Math (Java Platform SE 8 )
1 week ago - If the argument is equal to 10n for integer n, then the result is n. The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic. ... Returns the correctly rounded positive square root of a double value.
๐ŸŒ
GitHub
github.com โ€บ square โ€บ square-java-sdk
GitHub - square/square-java-sdk: Java client library for the Square API
Java client library for the Square API. Contribute to square/square-java-sdk development by creating an account on GitHub.
Starred by 66 users
Forked by 36 users
Languages ย  Java 100.0% | Java 100.0%
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ algorithms โ€บ creating a magic square in java
Creating a Magic Square in Java | Baeldung
January 8, 2024 - As it happens, there are three relatively simple algorithms that can be used to generate these, depending on the size of the square: ... Doubly: Even number of cells on each side. This is when each side is a multiple of 4. Singly: Even number of cells on each side. This is when each side is a multiple of 2 but not a multiple of 4. Weโ€™ll look at each of these algorithms and how to generate them in Java in a bit.
๐ŸŒ
Square
developer.squareup.com โ€บ docs โ€บ sdks โ€บ java โ€บ using-java-sdk
Using the Square Java SDK
In the com.squareup.square.models namespace, every object is a model. For example, complex types Address, Customer, and Order are all models defined in the namespace. You pass these models, with values, as parameters as shown in the following example. The example passes the Address model as a parameter to create a new customer. ... import java.util.UUID; ...