In Java, Math.sqrt(a) returns the square root of a: the number that gives a when multiplied by itself. Math.sqrt(144) is 12.0.
It is the right tool for distances, side lengths, the quadratic formula, and loops that only need to run up to the square root of a number. It has been available since the early versions of Java.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.sqrt(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass any number. It is widened to double, and the result is a double.
Java
double a = Math.sqrt(144); // 12.0
double b = Math.sqrt(2); // 1.4142135623730951
int r = (int) Math.sqrt(50); // 7 (the whole part)
double bad = Math.sqrt(-1); // NaN
Call
What it does
Math.sqrt(25)
Perfect square: returns 5.0.
Math.sqrt(-4)
Negative input: returns NaN.
(int) Math.sqrt(n)
Whole part of the root, used as a loop limit.
Math.sqrt(b * b - 4 * a * c)
Square root of the discriminant.
sqrt is one of the few methods Java requires to be correctly rounded: the result is the double nearest to the true root. That is why the roots of perfect squares come out exact.
Library declarations
Java
// java.lang.Math
public static double sqrt(double a)
Parameters
Parameter
Description
a
The value whose square root you want. It should be zero or positive.
Return Value
Return Value
Description
double
The positive square root of a. NaN if a is negative or NaN; Infinity for Infinity; a zero keeps its sign.
Exact for perfect squares. 12 * 12 is 144, and because sqrt is correctly rounded, the answer is exactly 12.0 with nothing left over.
Rounded for the rest. The true root of 2 never ends, so Java stores the nearest double, 1.4142135623730951; multiplying it by itself gives 2.0000000000000004, a hair above 2.
No exception for bad input. A negative number has no real root, so sqrt returns NaN; a negative zero keeps its sign, and infinity stays infinity.
Example 2 — Perfect-Square Check and Prime Test
Take the whole part of the root to test if a number is a perfect square, and to limit how far a prime-number loop must run.
Java
public class MathSqrtChecks {
static boolean isPerfectSquare(int n) {
if (n < 0) {
return false;
}
int r = (int) Math.sqrt(n);
return r * r == n;
}
static boolean isPrime(int n) {
if (n < 2) {
return false;
}
int limit = (int) Math.sqrt(n);
for (int i = 2; i <= limit; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] numbers = {49, 50, 91, 97};
for (int n : numbers) {
System.out.println(n + ": perfect square " + isPerfectSquare(n) + ", prime " + isPrime(n));
}
}
}
Output
49: perfect square true, prime false
50: perfect square false, prime false
91: perfect square false, prime false
97: perfect square false, prime true
How It Works
Cut off the decimals. The cast turns 7.0 into 7 for 49 and 7.07 into 7 for 50. Squaring that whole part gives back 49, but only 49 matches its own input.
A shorter loop. If a number has a divisor above its square root, it also has a partner below it, so testing 2 up to (int) Math.sqrt(n) is enough: 91 is caught at 7 (91 = 7 * 13).
Prime survives the whole loop. For 97 the limit is 9, and no value from 2 to 9 divides it, so the method reaches return true.
Example 3 — Solve a Quadratic Equation
Use the discriminant b * b - 4 * a * c to decide whether real roots exist before calling sqrt.
Java
public class MathSqrtQuadratic {
static void solve(double a, double b, double c) {
double disc = b * b - 4 * a * c;
if (disc < 0) {
System.out.println("No real roots (discriminant " + disc + ")");
return;
}
double root = Math.sqrt(disc);
System.out.println("x1 = " + (-b + root) / (2 * a));
System.out.println("x2 = " + (-b - root) / (2 * a));
}
public static void main(String[] args) {
solve(1, -5, 6);
solve(1, 2, 5);
}
}
Output
x1 = 3.0
x2 = 2.0
No real roots (discriminant -16.0)
How It Works
Positive discriminant. For x squared minus 5x plus 6, the discriminant is 25 - 24 = 1, and its root is 1.0, so the two answers are (5 + 1) / 2 = 3.0 and (5 - 1) / 2 = 2.0.
Guard before the root. For x squared plus 2x plus 5, the discriminant is 4 - 20 = -16.0; testing disc < 0 first avoids a silent NaN in the output.
Double arithmetic. The parameters are double, so the integer arguments 1, -5, and 6 are widened and every printed answer carries a .0.
Frequently Asked Questions
It returns the positive square root of a number as a double. Math.sqrt(144) returns 12.0.
It returns NaN. It does not throw an exception, so check for negative input yourself if it is possible.
Take int r = (int) Math.sqrt(n) and test r * r == n. For ordinary int values this is reliable, because sqrt is correctly rounded.
Cast the result: (int) Math.sqrt(50) gives 7. The cast drops the decimals; it does not round.
Yes. sqrt is easier to read and is guaranteed to be correctly rounded, while pow only promises a result within one unit in the last place.