.sqrt()
Published Sep 3, 2022
Contribute to Docs
The Math.sqrt()
method returns the positive, properly rounded square root of a double-type value.
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
), thenNaN
is returned. - If
num
is positive infinity (Double.POSITIVE_INFINITY
), thenInfinity
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.07.0
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours