Java打破了强类型!谁能解释一下?

发布于 2024-12-11 10:07:21 字数 811 浏览 0 评论 0原文

可能的重复:
可能导致精度损失的不同行为

我发现Java强中存在不一致在编译时输入检查。 请看下面的代码:

int sum = 0;
sum = 1; //is is OK
sum = 0.56786; //compile error because of precision loss, and strong typing
sum = sum + 2; //it is OK
sum += 2; //it is OK
sum = sum + 0.56787; //compile error again because of automatic conversion into double, and possible precision loss
sum += 0.56787; //this line is does the same thing as the previous line, but it does not give us a compile error, and javac does not complain about precision loss etc.

谁能给我解释一下吗?这是一个已知的错误,还是期望的行为? C++ 给出警告,C# 给出编译错误。

Java 会破坏强类型吗? 您可以将 += 替换为 -= 或 *= - 编译器可以接受所有内容。

Possible Duplicate:
Varying behavior for possible loss of precision

I found an inconsistence in Java strong typing check at compile time.
Please look at the following code:

int sum = 0;
sum = 1; //is is OK
sum = 0.56786; //compile error because of precision loss, and strong typing
sum = sum + 2; //it is OK
sum += 2; //it is OK
sum = sum + 0.56787; //compile error again because of automatic conversion into double, and possible precision loss
sum += 0.56787; //this line is does the same thing as the previous line, but it does not give us a compile error, and javac does not complain about precision loss etc.

Can anyone explain it to me? Is it a known bug, or desired behavior?
C++ gives a warning, C# gives a compile error.

Does Java breaks strong typing?
You can replace += with -= or *= - everything is acceptable by a compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无戏配角 2024-12-18 10:07:21

这种行为是由语言定义的(因此是可以的)。来自JLS

15.26.2 复合赋值运算符

E1 op= E2 形式的复合赋值表达式是等效的
到 E1 = (T)((E1) op (E2)),其中 T 是 E1 的类型,但 E1
仅评估一次。例如,下面的代码是正确的:

短 x = 3;
x+=4.6;

结果 x 的值为 7,因为它相当于:

短 x = 3;
x =(短)(x + 4.6);

This behaviour is defined by the language (and is therefore OK). From the JLS:

15.26.2 Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent
to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1
is evaluated only once. For example, the following code is correct:

short x = 3;
x += 4.6;

and results in x having the value 7 because it is equivalent to:

short x = 3;
x = (short)(x + 4.6);
梦途 2024-12-18 10:07:21

它可以编译,因为编译器正在转换

sum += 0.56787;

sum = (int)(sum + 0.56787);

It compiles because the compiler is converting

sum += 0.56787;

to

sum = (int)(sum + 0.56787);
傲世九天 2024-12-18 10:07:21

这与强类型无关,而仅与隐式转换的不同规则有关。

您在这里看到的是两个不同的运算符。在第一种情况下,您有简单的赋值运算符“=”,它不允许将 double 分配给 int。在第二种情况下,您有复合赋值运算符“+=”,它允许通过将 double 转换为 double 来将 double 添加到 int首先是int

This has nothing to do with strong typing but only with different rules for implicit conversions.

You are looking at two different operators here. In the first case, you have the simple assignment operator "=" which does not allow assigning a double to an int. In the second case, you have the compound assignment operator "+=" which allows adding a double to an int by converting the double to an int first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文