当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Double isNaN()用法及代码示例


Java Double类的isNaN()方法是Java中的内置方法,如果此Double值或指定的double值为Not-a-Number(NaN),则返回true,否则返回false。

用法

public boolean isNaN()
        or
public static boolean isNaN(double val)

参数:该函数接受单个参数val,该参数指定当直接使用Double类作为静态方法调用时要检查的值。当该方法用作实例方法时,不需要该参数。


返回值:如果val是NaN,则返回true;否则返回false。

以下程序说明了Java中的isNaN()方法:

示例1:

// Java code to demonstrate 
// Double isNaN() method 
// without parameter 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // first example 
        Double f1 = new Double(1.0 / 0.0); 
  
        boolean res = f1.isNaN(); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
  
        // second example 
        f1 = new Double(0.0 / 0.0); 
  
        res = f1.isNaN(); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
    } 
}
输出:
Infinity is not NaN
NaN is NaN

示例2:

// Java code to demonstrate 
// Double isNaN() method 
// with parameter 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // first example 
        Double f1 = new Double(1.0 / 0.0); 
  
        boolean res = f1.isNaN(f1); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
  
        // second example 
        f1 = new Double(0.0 / 0.0); 
  
        res = f1.isNaN(f1); 
  
        // printing the output 
        if (res) 
            System.out.println(f1 + " is NaN"); 
        else
            System.out.println(f1 + " is not NaN"); 
    } 
}
输出:
Infinity is not NaN
NaN is NaN

参考: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#isNaN()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Double isNaN() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。