私有静态最终双精度数为0
问题描述:
我正在尝试使用以下行指定一个双精度常量,有人可以帮我解释一下为什么在运行时此常量的值为0.0
:
I am trying to use the following line to specify a double constant, can anybody help explain to me why at runtime this constant has a value of 0.0
:
private static final double CONSTANT = 1/2;
答
1和2被解释为整数,并产生除法的整数结果.在末尾添加D,以使它们解释为双精度.
1 and 2 are interpreted as integers and produce integer result of division. Add D at the end to make them interpreted as doubles.
private static final double CONSTANT = 1D/2D;