Java Math sqrt()

The sqrt() method returns the square root of the specified number.

Example


Syntax of Math.sqrt()

The syntax of the sqrt() method is:

Math.sqrt(double num)

Here, sqrt() is a static method. Hence, we are accessing the method using the class name, Math.


sqrt() Parameters

The sqrt() method takes a single parameter.

  • num - number whose square root is to be computed

sqrt() Return Values

  • returns square root of the specified number
  • returns NaN if the argument less than 0 or NaN

Note: The method always returns the positive and correctly rounded number.


Example: Java Math sqrt()

In the above example, we have used the Math.sqrt() method to compute the square root of infinity, positive number, negative number, and zero.

Here, Double.POSITIVE_INFINITY is used to implement positive infinity in the program.

When we pass an int value to the sqrt() method, it automatically converts the int value to the double value.

int a = 36;

Math.sqrt(a);   // returns 6.0

Recommended Tutorials