Java Math sqrt() Method Last Updated : 13 May, 2025 Comments Improve Suggest changes Like Article Like Report The Math.sqrt() method is a part of java.lang.Math package. This method is used to calculate the square root of a number. This method returns the square root of a given value of type double. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Special Cases:If the argument is positive, then the method will return the square root of the number.If the argument is negative, then the method will return NaN.If the argument is positive infinity, then the method will return positive infinity.If the argument is NaN, it returns NaN.If the argument is zero, then the method will return zero.These special cases make sure that the Math.sqrt() methods work correctly.Syntax of sqrt() Methodpublic static double sqrt(double a)Parameter: This method takes a single parameter a, of type double, whose square root need to be calculated.Return Type: This method returns the positive square root value of the argument passed to it. And for the special cases, we mentioned above.Now, we are going to discuss some examples for better understanding and clarity.Examples of Java Math sqrt() MethodExample 1: In this example, we will see the square root of positive numbers. Java // Java program to demonstrate the // working of Math.sqrt() method import java.lang.Math; class Geeks { public static void main(String args[]) { double a = 30; System.out.println(Math.sqrt(a)); a = 45; System.out.println(Math.sqrt(a)); a = 60; System.out.println(Math.sqrt(a)); a = 90; System.out.println(Math.sqrt(a)); } } Output5.477225575051661 6.708203932499369 7.745966692414834 9.486832980505138 Explanation: Here, we are calculating the square root of different numbers like 30, 45, 60 and 90.Example 2: In this example, we will see how the sqrt() method handles special cases like NaN, negative values and posititve infinity. Java // Java program to demonstrate working // of Math.sqrt() method for special cases import java.lang.Math; public class Geeks { public static void main(String[] args) { double p = Double.POSITIVE_INFINITY; double n = -5; double nan = Double.NaN; double res; // Here the agument is negative res = Math.sqrt(n); System.out.println(res); // Here the argument is positive infinity res = Math.sqrt(p); System.out.println(res); // Here the argument is NaN res = Math.sqrt(nan); System.out.println(res); } } OutputNaN Infinity NaN Explanation: Here, we are handling special cases such as NaN and Infinity. When the given argument is negative and NaN then we will get NaN as an output, and when the argument is positive infinity then we will get infinity. Comment More infoAdvertise with us Next Article BigInteger sqrt() Method in Java B barykrg Follow Improve Article Tags : Java Java-lang package java-math Practice Tags : Java Similar Reads java.math class and its methods | Set 1 Math class provides mathematical functions to perform basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. cosh, sin, tan, abs, bitLength, multiply and many more. Implementations of the functions of Math class do not return bit-for-bit same results. Henc 4 min read Java Math hypot() method with Example The Math.hypot() function is an in-built Math function in Java. Before going deep dive to the topic, first understand the reason behind to use this function.Suppose we have a right-angled triangle and we know the lengths of the two shorter sides i.e., x and y. To calculate the hypotenuse, we use sqr 2 min read StrictMath sqrt() Method in Java The java.lang.StrictMath.sqrt() is an inbuilt method of StrictMath class in Java which is used to obtain the exact rounded positive square root of a specified double value. Syntax: public static double sqrt(double num) Parameters: The function accepts a single parameter num of double type, whose squ 2 min read Java sqrt() method with Examples The Math.sqrt() method is a part of java.lang.Math package. This method is used to calculate the square root of a number. This method returns the square root of a given value of type double. In this article, we are going to discuss how this method works for regular values and for special cases such 2 min read BigInteger sqrt() Method in Java java.math.BigInteger.sqrt() is an inbuilt function added in Java SE 9 & JDK 9 which returns BigInteger value of square root of a BigInteger on which sqrt() method is applied. It is the same as the floor(sqrt(n)) where n is a number. This Square root is less than the real square root if the real 2 min read Math pow() Method in Java with Example The Math.pow() method in Java is used to calculate a number raised to the power of some other number. It is part of the java.lang.Math class and is widely used in mathematical computations, scientific calculations, and algorithmic problems. This function accepts two parameters and returns the value 3 min read StrictMath cbrt() Method in Java The java.lang.StrictMath.cbrt() is an inbuilt method in Java which is used to return the cube root of a given double value. The method shows three special results: The result is a zero with the same sign as the argument when the given argument is zero.The result is infinity with the same sign of arg 2 min read Math class methods in Java with Examples | Set 2 java.math class and its methods | Set 1 java.math class methods discussed in this article : abs() : java.math.abs() method returns the absolute value of any type of argument passed. This method can handle all the data types. Special Case : Result is positive zero, if the argument is positive zero or 6 min read StrictMath getExponent() Method In Java The getExponent(double num) is an inbuilt method of StrictMath class which is used to get the unbiased exponent to be used in the representation of a given double argument. It gives rise to two special results: The result will be Double.MAX_EXPONENT + 1 when the given argument is NaN or infinite The 2 min read Java cbrt() method with Examples The Math.cbrt() is a part of java.lang.Math package. This method is used to calculate the cube root of a given number. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Note: A number, when multiplied by itself three time 3 min read Like