SwadhyAI Notes

All Java notes

How to Find Square Root in Java √

The Challenge

Calculate the square root of a number using Java's Math class!

Solution

Using Math.sqrt()

java
public class Main {
    public static void main(String[] args) {
        double number = 64;
        double squareRoot = Math.sqrt(number);
        
        System.out.println("Square root of " + number + " = " + squareRoot);
        // Output: Square root of 64.0 = 8.0
    }
}

More Examples

Perfect Squares

java
System.out.println(Math.sqrt(4));    // 2.0
System.out.println(Math.sqrt(9));    // 3.0
System.out.println(Math.sqrt(16));   // 4.0
System.out.println(Math.sqrt(25));   // 5.0
System.out.println(Math.sqrt(100));  // 10.0

Non-Perfect Squares

java
System.out.println(Math.sqrt(2));    // 1.4142135623730951
System.out.println(Math.sqrt(3));    // 1.7320508075688772
System.out.println(Math.sqrt(50));   // 7.0710678118654755

Rounding the Result

java
double number = 50;
double sqrt = Math.sqrt(number);

// Round to 2 decimal places
double rounded = Math.round(sqrt * 100.0) / 100.0;
System.out.println("Rounded: " + rounded); // 7.07

Did You Know?

The square root of a negative number in Java returns NaN (Not a Number). For complex numbers, you'd need a special library!

java
System.out.println(Math.sqrt(-1)); // NaN

Pro Tip

Use Math.pow() for other roots:

java
// Cube root (∛)
double cubeRoot = Math.pow(27, 1.0/3.0); // 3.0

// Fourth root (∜)
double fourthRoot = Math.pow(16, 1.0/4.0); // 2.0

Real-World Use Cases

  • Geometry: Calculate diagonal of a square
  • Physics: Calculate velocity, distance formulas
  • Graphics: Calculate distances between points
  • Statistics: Standard deviation calculations
›Short versionOpen if you only have a minute.
  • Math.sqrt returns a double. Math.sqrt of a negative input is NaN, not an exception.
  • Casting that double to int truncates. 2.9 becomes 2.
  • A long perfect square past the 53-bit mantissa is not exact in double, so a cast of Math.sqrt can be off by one.
  • For an integer root, binary search and then square the candidate in long.

Square root

Compute the principal square root of a non-negative number.

Working approach

  • Use Math.sqrt for a decimal result, after deciding what a negative input should do.
  • Cast only at the end, and only if the caller asked for a truncated integer.
  • Check a perfect square by squaring a long candidate and comparing, not by trusting the cast.

Walk the sample

  1. Input 49
  2. Math.sqrt(49) returns 7.0
  3. The stored result is 7.0

The trap

Math.sqrt(-1) does not throw. Callers that cast NaN to int get 0, which looks like a root.

java
static double squareRoot(double value) {
    if (value < 0) {
        throw new IllegalArgumentException("square root of a negative");
    }
    return Math.sqrt(value);
}

// (int) Math.sqrt(-1) is 0, because (int) NaN is 0

What does (int) Math.sqrt(-1.0) evaluate to?

Math.sqrt(-1.0) is NaN. Casting NaN to int yields 0.

A missing negative check therefore returns a plausible-looking root. Reject the negative before the call if the program should not continue.

Math.sqrt is O(1) time and O(1) space. An integer binary search is O(log n).

Easy to get wrong

  • Casting to int before you needed a whole number, which drops the fraction.
  • Treating NaN as a successful root.