×

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value.

Syntax:

public static double sqrt(double a)

Parameters:

The parameter ‘a’ represents the number whose square root is to be determined.

Return Value:

The sqrt() method returns the positive square root of ‘a’.

  • It returns zero with same sign as argument, if the argument is positive or negative zero.
  • It returns infinity with the same sign as the argument, if the argument is infinite.
  • It returns NaN, if the argument is NaN or less than zero.

Example 1:

public class JavaMathSqrtExample1 {
    public static void main(String[] args) {
        double a=4.0;
        //returns the square root of a double value
        System.out.println("Square root of "+ a +" = "+Math.sqrt(a));
    }
}

Output:

Square root of 4.0 = 2.0

Example 2:

public class JavaMathSqrtExample2 {
    public static void main(String[] args) {
        double a=Double.NaN;
        //returns the squre root of a NaN value
        System.out.println("Square root of "+ a +" = "+Math.sqrt(a));
    }
}

Output:

Square root of NaN = NaN

Example 3:

public class JavaMathSqrtExample3 {
    public static void main(String[] args) {
        double a=-0.0;
        //returns a zero with the same sign as the argument ,if the argument is zero.
        System.out.println("Square root of "+ a +" = "+Math.sqrt(a));
    }
}

Output:

Square root of -0.0 = -0.0

Example 4:

public class JavaMathSqrtExample4 {
    public static void main(String[] args) {
        double a=-5/0.0;
        //returns NaN,if the argument is less than zero.
        System.out.println("Square root of "+ a +" = "+Math.sqrt(a));
    }
}

Output:

Square root of -Infinity = NaN

Example 5:

public class JavaMathSqrtExample5 {
    public static void main(String[] args) {
        double a=Double.POSITIVE_INFINITY;
        //returns Infinity,if the argument is Positive infinity.
        System.out.println("Square root of "+ a +" = "+Math.sqrt(a));
    }
}

Output:

Square root of Infinity = Infinity

Related Topics

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.