# Math.sqrt() Method In Java

Math.sqrt() Method In Java | The sqrt() method of java.lang.Math class is given to calculate the square root value of a given number. It returns the correctly rounded positive square root of a double value.

The prototype of java.lang.Math.sqrt() method is:-
`public static double sqrt(double a)`

Argument type:- double
Argument:- a number
Return type:- double
Return value:- positive square root of the passed argument.

Since the sqrt() method is a static method so we can call it directly through its class name like Math.sqrt(). Or for an easy purpose, we can import all static members of the Math class using the “import static” statement.

## Java Math.sqrt() Examples

``````public class SquareRoot {
public static void main(String[] args) {

// take a number
int num1 = 25;

// find square root value
double result = Math.sqrt(num1);

// display result
System.out.println(result);
}
}``````

Output:-

5.0

Other examples,

If we import the Math class statically and then we can invoke sqrt() method without calling through its class name.

``````// static import
import static java.lang.Math.*;
public class SquareRoot {
public static void main(String[] args) {

// find square root value
// invoke sqrt() without class name
double result = sqrt(81);

// display result
System.out.println(result);
}
}``````

Output:-

9.0

The “import static java.lang.Math.*;” statement will import all static members of the Math class. But if we want to import only the sqrt() method of the Math class, not another static method and variables of the Math class then we can use the “import static java.lang.Math.sqrt;” statement. Learn more about static import in Java

``````// static import
import static java.lang.Math.sqrt;
public class SquareRoot {
public static void main(String[] args) {

// find square root value
// invoke sqrt() without class name
double result = sqrt(20);

// display result
System.out.println(result);
}
}``````

Output:-

4.47213595499958

## Special Cases

• If the argument is NaN or less than zero, then the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• And if the argument is positive zero or negative zero, then the result is the same as the argument.
• Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

`//` the argument is less than zero
`System.out.println(sqrt(-81));`

Output:- NaN

`//` argument is NaN
`System.out.println(sqrt(Double.NaN));`

Output:- NaN

`//` the argument is positive infinity
`System.out.println(sqrt(Double.POSITIVE_INFINITY));`

Output:- Infinity

// the argument is positive infinity
`System.out.println(sqrt(Double.NEGATIVE_INFINITY));`

Output:- NaN

`//` argument is zero
`System.out.println(sqrt(0));`

Output:- 0.0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!