且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java显式类型转换为浮点除法

更新时间:2023-02-12 23:31:29

不,这不是显式的类型转换.您可能想要使用以下内容:

No, that's not an explicit typecast. You would want to use something like this:

result = ((double) num1) / ((double) num2);

实际上,由于/运算符的扩展规则,您只需要这些显式强制转换之一,但是同时拥有这两个名称并没有什么害处.实际上,由于强制转换运算符()的优先级高于除法运算符/,因此可以将其写为:

Actually, because of the widening rules for the / operator, you would only need one of those explicit casts, but there's no harm in having both. In fact, because the cast operator () has higher precedence than the division operator /, you could write it as:

result = (double) num1 / num2;

结合了分子的显式转换和分母的隐式转换.

which combines an explicit cast of the numerator and an implicit cast of the denominator.