×

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

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Arithmetic Operations on String in Java

Introduction Arithmetic, Relational, Bitwise, and Logical operators are all available in Java. Simple mathematical calculations are performed using Java arithmetic operators. Basic Arithmetic operators are considered in Java to be Addition,...

4 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.