Java 浮点/整数隐式转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4097694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
float/int implicit conversion
提问by Rich
I'm doing multiplication and division of float
s and int
s and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here).
我正在做float
s 和int
s 的乘法和除法,我忘记了隐式转换规则(问题中的词似乎太模糊了,谷歌比在这里问得更快)。
If I have two int
s, but I want to do floating-point division, do I need only to cast one or both of the operands? How about for multiplication — if I multiply a float
and an int
, is the answer a float
?
如果我有两个int
s,但我想进行浮点除法,我是否只需要强制转换一个或两个操作数?乘法如何——如果我将 afloat
和 an相乘int
,答案是 afloat
吗?
采纳答案by Damian Leszczyński - Vash
You can't assign to an int
result from division of a float
by an int
or vice-versa.
您不能将int
afloat
除以a分配给结果,int
反之亦然。
So the answers are:
所以答案是:
If I have two
int
s, but I want to do floating point division…?
如果我有两个
int
s,但我想做浮点除法……?
One cast is enough.
一个演员就够了。
If I multiply a
float
and anint
, is the answer afloat
?
如果我将 a
float
和 an相乘int
,答案是 afloat
吗?
Yes it is.
是的。
float f = 1000f;
int i = 3;
f = i; // Ok
i = f; // Error
f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)
i = i/f; //Error
i = f/i; //Error
回答by Matt Ball
In order to perform any sort of floating-point arithmetic with integers, you need to convert (read: cast) at least one of the operands to a float
type.
为了对整数执行任何类型的浮点运算,您需要将至少一个操作数转换(读取:强制转换)为一种float
类型。
回答by Bozho
To demonstrate:
展示:
int i1 = 5;
float f = 0.5f;
int i2 = 2;
System.out.println(i1 * f);
System.out.println(i1 / i2);
System.out.println(((float) i1) / i2);
Result:
结果:
2.5
2
2.5
回答by blizpasta
If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral.
如果二元运算符的至少一个操作数是浮点类型,则该操作是浮点操作,即使另一个是整数。
(Source: Java language specifications - 4.2.4)
(来源:Java 语言规范 - 4.2.4)
if I multiply a float and an int, is the answer a float?
如果我将一个浮点数和一个整数相乘,答案是一个浮点数吗?
System.out.println(((Object)(1f*1)).getClass());//class java.lang.Float
(If you use DrJava, you can simply type ((Object)(1f*1)).getClass() into the interactions panel. There's a plugin for DrJava for Eclipse too.)
(如果你使用 DrJava,你可以简单地在交互面板中输入 ((Object)(1f*1)).getClass() 。也有一个 DrJava for Eclipse 插件。)
回答by user207421
The simple answer is that Java will perform widening conversions automatically but not narrowing conversions. So for example int->float is automatic but float->int requires a cast.
答案很简单,Java 会自动执行扩大转换,但不会自动执行缩小转换。因此,例如 int->float 是自动的,但 float->int 需要强制转换。
回答by supercat
Java ranks primitive types in the order int < long < float < double
. If an operator is used with different primitive types, the type which appears before the other in the above list will be implicitly converted to the other, without any compiler diagnostic, even in cases where this would cause a loss of precision. For example, 16777217-1.0f
will yield 16777215.0f (one less than the correct value). In many cases, operations between a float
and an int
outside the range +/-16777216 should be performed by casting the int
to double
, performing the operation, and then--if necessary--casting the result to float
. I find the requirement for the double casting annoying, but the typecasting rules in Java require that one either use the annoying double cast or suffer the loss of precision.
Java 按顺序排列原始类型int < long < float < double
。如果一个运算符与不同的原始类型一起使用,则上面列表中出现在另一个之前的类型将被隐式转换为另一个,无需任何编译器诊断,即使在这会导致精度损失的情况下也是如此。例如,16777217-1.0f
将产生 16777215.0f(比正确值少 1)。在许多情况下,范围 +/-16777216 之外的afloat
和a 之间的操作int
应该通过将 转换int
为double
,执行操作,然后(如有必要)将结果转换为 来执行float
。我发现双重转换的要求很烦人,但是 Java 中的类型转换规则要求人们要么使用烦人的双重转换,要么遭受精度损失。