🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Math sqrt() method in Java programming and how to use it with an example.
1. Math sqrt() Method Overview
Definition:
The sqrt() method of Java's Math class is used to calculate the square root of a given number.
Syntax:
Math.sqrt(double a)
Parameters:
- a: The value whose square root needs to be computed.
Key Points:
- If the argument a is NaN or less than zero, then the result will be NaN.
- If the argument a is positive infinity, then the result will be positive infinity.
- If argument a is zero, then the result will be zero.
- It returns the positive square root of a double value.
2. Math sqrt() Method Example
public class SqrtExample {
public static void main(String[] args) {
System.out.println("Square root of 16: " + Math.sqrt(16));
System.out.println("Square root of 0: " + Math.sqrt(0));
System.out.println("Square root of 7: " + Math.sqrt(7));
System.out.println("Square root of 2.25: " + Math.sqrt(2.25));
System.out.println("Square root of -9 (will result in NaN): " + Math.sqrt(-9));
}
}
Output:
Square root of 16: 4.0 Square root of 0: 0.0 Square root of 7: 2.6457513110645907 Square root of 2.25: 1.5 Square root of -9 (will result in NaN): NaN
Explanation:
In the example:
1. The square root of 16 is 4.
2. The square root of 0 is 0.
3. The square root of 7 is approximately 2.6457513110645907.
4. The square root of 2.25 is 1.5.
5. Since we can't compute the square root of a negative number using this method, it results in NaN (Not a Number).
Related Java Math class method examples
- Java Math abs() example
- Java Math ceil() example
- Java Math floor() example
- Java Math max() example
- Java Math pow() example
- Java Math sqrt() example
- Java Math random() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment