Java .sqrt()

Yash-Var's avatar
Published Sep 3, 2022
Contribute to Docs

The Math.sqrt() method returns the positive, properly rounded square root of a double-type value.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.sqrt(double num)

The return type of the .sqrt() method is a double. The data type of the num parameter is also a double. However, if a decimal is not provided, num will still be treated like a double-type.

Some edge cases for the .sqrt() method include the following:

  • If num is less than zero or it is not a numeric type (NaN), then NaN is returned.
  • If num is positive infinity (Double.POSITIVE_INFINITY), then Infinity is returned.

Example

The following example showcases the .sqrt() method being applied to two variables:

public class Check {
public static void main(String args[]) {
double x = 64.0;
System.out.println(Math.sqrt(x));
double y = 49.0;
System.out.println(Math.sqrt(y));
}
}

This results in the following output:

8.0
7.0

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours