Java.lang Package Classes

Java Math - sqrt() Method



The java.lang.Math.sqrt() method returns the square root of the given number. In special cases it returns the following:

  • 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.
  • If the argument is positive zero or negative zero, then the result is the same as the argument.

Syntax

public static double sqrt(double a)

Parameters

a Specify a value.

Return Value

Returns the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

Exception

NA.

Example:

In the example below, sqrt() method is used to find out the square root of the given number.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.sqrt(25)); 
  System.out.println(Math.sqrt(30)); 
  System.out.println(Math.sqrt(35.5)); 
  System.out.println(Math.sqrt(-25));     
 }
}

The output of the above code will be:

5.0
5.477225575051661
5.958187643906492
NaN

❮ Java.lang - Math