🎓 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
The Double.isNaN() method in Java is used to determine if a Double object or a primitive double value is a Not-a-Number (NaN) value.
Table of Contents
- Introduction
isNaN()Method Syntax- Examples
- Checking NaN Values
- Handling Non-NaN Values
- Using the Static Method
- Real-World Use Case
- Conclusion
Introduction
The Double.isNaN() method is used to check whether a given Double object or primitive double value is NaN. NaN is a special floating-point value used to represent undefined or unrepresentable values, such as the result of 0.0/0.0.
isNaN()() Method Syntax
The Double.isNaN() method has two versions:
Instance Method
public boolean isNaN()
- This method is called on a
Doubleobject to check if it represents NaN.
The method returns:
trueif theDoubleobject represents NaN.falseotherwise.
Static Method
public static boolean isNaN(double v)
- v: The primitive
doublevalue to be tested.
The method returns:
trueif the specified value is NaN.falseotherwise.
Examples
Checking NaN Values
The isNaN(double v) method can be used to check if a double value is NaN.
Example
public class NaNExample {
public static void main(String[] args) {
double value = Double.NaN;
boolean isNaN = Double.isNaN(value);
System.out.println("Is NaN: " + isNaN);
}
}
Output:
Is NaN: true
In this example, the method checks if the value Double.NaN is NaN.
Handling Non-NaN Values
The isNaN() method returns false for values that are not NaN.
Example
public class NonNaNExample {
public static void main(String[] args) {
double value = 123.45;
boolean isNaN = Double.isNaN(value);
System.out.println("Is 123.45 NaN: " + isNaN);
}
}
Output:
Is 123.45 NaN: false
In this example, the method checks if the value 123.45 is NaN, and it returns false.
Using the Instance Method
The isNaN() instance method can be used to check if a Double object represents NaN.
Example
public class DoubleObjectExample {
public static void main(String[] args) {
Double nanValue = Double.NaN;
Double normalValue = 123.45;
System.out.println("Is NaN value NaN: " + nanValue.isNaN());
System.out.println("Is 123.45 NaN: " + normalValue.isNaN());
}
}
Output:
Is NaN value NaN: true
Is 123.45 NaN: false
In this example, the instance method is used to check if Double objects represent NaN.
Real-World Use Case
Validating User Input
In a real-world application, you might need to validate user input to ensure it is not NaN before performing calculations.
Example
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
double inputValue = scanner.nextDouble();
if (Double.isNaN(inputValue)) {
System.out.println("Invalid input: Not-a-Number (NaN)");
} else {
System.out.println("Valid input: " + inputValue);
}
scanner.close();
}
}
Output (example input NaN):
Enter a number:
Invalid input: Not-a-Number (NaN)
In this example, the code checks if the user input is NaN and prints an appropriate message.
Conclusion
The Double.isNaN() method in Java is used for detecting NaN values in floating-point operations. By understanding how to use this method and its overloaded versions, you can efficiently handle tasks that involve checking for NaN values in your Java applications. Whether you are dealing with mathematical operations, validating user input, or handling special cases, the isNaN() method provides a reliable solution for these tasks.
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