[英]Initialize a double and its getter
What benefits has the following? 以下是什么好处?
private double pro;
pro = Double.NaN;
Why not make it 为什么不成功呢
pro = 0.0;
the getter is: 吸气剂是:
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. 因为NanN
代表“不是数字”,所以通常暗示该数字尚未初始化。
You could use pro = 0.0;
你可以使用pro = 0.0;
and that would be fine, too. 那也没关系。 I think that the issue is more of a style-thing. 我认为这个问题更像是一种风格。
Because any operation on NaN results NaN. 因为对NaN的任何操作都会导致NaN。 While operations with 0.0
may result as if pro
is set to some value. 使用0.0
操作可能会导致pro
被设置为某个值。 While NaN may be used to represents that pro
isn't defined yet as your case. 虽然NaN可能用于表示pro
尚未定义为您的情况。
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 而d=0.0
会产生类似的结果
0.0
Infinity
0.0
10.0
我想在你的情况下, NaN
是一个提示,因为变量pro
没有被初始化,0.0是一个有效的初始化值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.