What is Math.sqrt() in Java?

The sqrt() function returns the square root of a double value sent as a parameter.

Figure 1, below, shows the mathematical representation of the sqrt() function.

Figure 1: Mathematical representation of the sqrt() function

Syntax

double sqrt(double num)

Parameter

This function requires a double value as a parameter.

Return Value

sqrt() returns the square root of a double value sent as a parameter.

If the argument is NaN or 0, then the function returns NaN.

Example

class JAVA {
public static void main( String args[] ) {
//integer
System.out.println("Math.sqrt(2):");
System.out.println(Math.sqrt(2));
//perfect square root
System.out.println("Math.sqrt(9):");
System.out.println(Math.sqrt(9));
//negative number
System.out.println("Math.sqrt(-4):");
System.out.println(Math.sqrt(-4));
}
}

Free Resources