开发者

Initialize a double and its getter

开发者 https://www.devze.com 2023-02-18 08:36 出处:网络
Whatbenefits has the following? private double pro; pro = Double.NaN; Why not make it pro = 0.0; the getter is:

What benefits has the following?

private double pro;
pro = Double.NaN;

Why not make it

pro = 0.0;

the getter is:

开发者_JS百科
public double getpro() {
    if (Double.isNaN(pro))
        somemethod();
    return pro;
}


Because NanN stands for "not a number," it is generally a hint that the number hasn't been initialized.

You could use pro = 0.0; and that would be fine, too. I think that the issue is more of a style-thing.


I guess that in your case, NaN is a hint for the variable pro is not initialized and 0.0 is a valid initialized value.


Because any operation on NaN results NaN. While operations with 0.0 may result as if pro is set to some value. While NaN may be used to represents that pro isn't defined yet as your case.

     Double d =Double.NaN;
     System.out.println(d*10);
     System.out.println(10/d);
     System.out.println(d/10);
     System.out.println(d+10);

results

NaN
NaN
NaN
NaN

while d=0.0 would result something like

0.0
Infinity
0.0
10.0
0

精彩评论

暂无评论...
验证码 换一张
取 消