🎓 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.
Comments
Post a Comment
Leave Comment