本文介绍了私有静态最终双精度数为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;

这篇关于私有静态最终双精度数为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:45