Library contents

Java Math.sqrt

In Java, Math.sqrt returns the nonnegative square root of a nonnegative number. The square root of 9 is 3 because 3 × 3 = 9.

A simple example

Math.sqrt

  1. Start with 9
  2. Do this Find the nonnegative square root
  3. Result 3
Three times three equals nine.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.sqrt(9.0));
    }
}
Output
3.0

Syntax

Java syntax
Math.sqrt(area)
JDK 21 method signatures (1)
public static double sqrt(double a)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Find the side of a square garden

A square floor plan is specified by its area, but the supplier asks for the length of one side. Recover the side length so the same measurement works along either edge.

From the programdouble area = 81.0
Result9.000
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        double area = 81.0;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.sqrt(area)));
    }
}
Output
9.000

A garden covering 81 square metres has sides of 9 metres because 9 times 9 is 81.

Advertisements

Estimate the side of a square meeting room

A meeting-room drawing gives the area of a square floor. Calculate the length of a wall from that area before ordering trim for the room.

From the programdouble area = 144.0
Result12.000
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        double area = 144.0;

        System.out.printf(java.util.Locale.ROOT, "%.3f%n", (double) (Math.sqrt(area)));
    }
}
Output
12.000

A 144-square-metre meeting room needs 12 metres of skirting along each side.

Lay out a square photo wall

A gallery reserves 49 square metres for a square photo display. The designer needs the length of one edge to mark the plan.

GivenSquare area 49 m²
Worked result7 m

Seven metres times seven metres covers the specified area.

What to watch for

A real square root requires a nonnegative area.

Documentation for Math.sqrt