Examples
Let's look at a few examples to see how `Math.sqrt()` can be used in practice.
Example 1: Finding the square root of a positive number
Java
double number = 25;
double squareRoot = Math.sqrt(number);
System.out.println("The square root of " + number + " is " + squareRoot);

You can also try this code with Online Java Compiler
Run Code
Output
The square root of 25.0 is 5.0
In this example, we find the square root of the number 25 using `Math.sqrt()`. The result, which is 5, is stored in the `squareRoot` variable and then printed to the console.
Example 2: Finding the square root of a variable
Java
int x = 16;
double squareRoot = Math.sqrt(x);
System.out.println("The square root of " + x + " is " + squareRoot);

You can also try this code with Online Java Compiler
Run Code
Output
The square root of 16 is 4.0
Here, we demonstrate that `Math.sqrt()` can also work with variables. We store the value 16 in the `x` variable and then pass `x` to `Math.sqrt()`. The result is stored in the `squareRoot` variable and printed to the console.
Example 3: Calculating the hypotenuse of a right triangle
Java
double base = 3;
double height = 4;
double hypotenuse = Math.sqrt(base * base + height * height);
System.out.println("The hypotenuse is " + hypotenuse);

You can also try this code with Online Java Compiler
Run Code
Output
The hypotenuse is 5.0
In this example, we use `Math.sqrt()` to calculate the hypotenuse of a right triangle using the Pythagorean theorem. We have the base and height of the triangle stored in variables, and we compute the square of each using multiplication. Then, we add the squares together and pass the result to `Math.sqrt()` to find the hypotenuse.
Frequently Asked Questions
What happens if I try to find the square root of a negative number using Math.sqrt()?
If you pass a negative number to Math.sqrt(), it will return NaN (Not-a-Number), indicating that the result is undefined in the real number system.
Can I use Math.sqrt() with other numeric data types besides double?
Yes, you can use Math.sqrt() with float, int, and long data types. However, the method always returns a double value, so you may need to cast the result back to the desired data type.
Is there any performance difference between using Math.sqrt() and calculating the square root manually?
Math.sqrt() is a highly optimized method implemented in the Java standard library. It is generally faster and more efficient than calculating the square root manually using techniques like the Babylonian method or Newton's method.
Conclusion
In this article, we learned about the Math.sqrt() method in Java, which provides an easy way to find the square root of a number. We discussed the syntax of Math.sqrt() and looked at several examples showing its usage in different scenarios.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.