简体   繁体   中英

Double.NaN is an object

public class Double1 {    

    public static double parseDouble(String _s, double _def) {
        try {
            return Double.parseDouble(_s);
        }
        catch(Exception e) {
        }
        return _def;
    }

    public static void main(String[] args) {
        Double1 db=new Double1();
        boolean ab=db.parseDouble("vijay", Double.NaN)!=Double.NaN?true:false;
        System.out.println("ab value: "+ ab);
        System.out.println(Double.NaN==Double.NaN);
    }
}

It should return true where as the above code returns false . Why?

NaN's compare false to everything - including themselves.

You can check for NaN with

Double.isNaN(doubleValue)

Which actually does nothing other than using exactly this behavior: A value x is a NaN if x != x .

but it's normal. Your parseDouble method tries to parse "vijay" and returns _def because "vijay" is not a double value.And db.parseDouble("vijay", Double.NaN) will return Double.NaN an finally Double.NaN!=Double.NaN is false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM