java中的Arith溢出:为什么运行时没有异常,也没有编译器警告?
我知道这个问题已经部分回答这里,但他们解释了什么发生在算术溢出期间。在其他地方,他们解释了 如何检查 java 代码中的溢出,即作为程序员防止溢出。
这里我想问为什么运行时没有异常,也没有编译器警告?
AFAIK,这个问题之前还没有得到回答。
在 Bruce Eckel 的书 Thinking in Java,第一版中。 (2000),第 3 章,有一个 java 小程序:
//: Overflow.java
// Surprise! Java lets you overflow.
public class Overflow {
public static void main(String[] args) {
int big = 0x7fffffff; // max int value
prt("big = " + big);
int bigger = big * 4;
prt("bigger = " + bigger);
}
static void prt(String s) {
System.out.println(s);
}
} ///:~
其输出是:
<前><代码>大 = 2147483647 更大=-4并且您不会从编译器中得到任何错误或警告,也没有异常 在运行时。
当使用 Integer.MAX_VALUE 而不是“0x7fffffff”时没有变化
我已经在 1.2 到 1.6 的一些 java 编译器上尝试过这一点,它仍然显示这种行为。我现在想知道为什么会这样?
- 这是错误还是功能?
- 这是否无法检测到,或者对于编译器设计者来说是一个低优先级的问题?
- 这是因为向后兼容性而没有修复吗?
- 也许在较新版本的 JDK 中,可以通过启用一些很少使用的编译器开关/-D:property 或一些 java VM 参数 (-XX:...) 在编译时进行控制吗?
刚才我使用了这些虚拟机、Windows x86 32 位
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
和
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
By the way, C# (Microsoft.CSharp\v4.0_4.0.0.0) shows the same behaviour
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OverflowCheck
{
class Overflow
{
static void Main(string[] args)
{
int big = 0x7fffffff; // same behaviour vor Int32.MaxValue
prt("big = " + big);
int bigger = big * 4;
prt("bigger = " + bigger);
prt("done");
}
static void prt(String s) {
System.Console.WriteLine(s);
}
}
}
输出:
big = 2147483647
bigger = -4
I know this question has been partly answered here on S.O., but there they explain what happens during an arithmetic overflow. And elsewhere on S.O. they explain how to check for overflow in java code, i.e. to prevent it as a programmer.
Here I am asking why there are no exceptions at run-time, and no compiler warnings?
AFAIK, this has not been answered before.
In Bruce Eckel's book Thinking in Java, 1st ed. (2000), Chapter 3, there is this little java program:
//: Overflow.java
// Surprise! Java lets you overflow.
public class Overflow {
public static void main(String[] args) {
int big = 0x7fffffff; // max int value
prt("big = " + big);
int bigger = big * 4;
prt("bigger = " + bigger);
}
static void prt(String s) {
System.out.println(s);
}
} ///:~
The output of this is:
big = 2147483647 bigger = -4
and you get no errors or warnings from the compiler, and no exceptions
at run-time.
No change when Integer.MAX_VALUE is used instead of "0x7fffffff"
I have tried this on some java compilers from 1.2 to 1.6, and it still shows this behaviour. I now wonder why this is so?
- Is this a bug or feature?
- Is this impossible to detect, or a low-priority issue for compiler designers?
- Is this not fixed because of backward compatibility?
- Maybe in newer releases of the JDK, can this be controlled at compile time by enabling some rarely used compiler switch/-D:property, or some java VM argument (-XX:...)?
Just now I used these VMs, Windows x86 32 bit
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
and
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
By the way, C# (Microsoft.CSharp\v4.0_4.0.0.0) shows the same behaviour
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OverflowCheck
{
class Overflow
{
static void Main(string[] args)
{
int big = 0x7fffffff; // same behaviour vor Int32.MaxValue
prt("big = " + big);
int bigger = big * 4;
prt("bigger = " + bigger);
prt("done");
}
static void prt(String s) {
System.Console.WriteLine(s);
}
}
}
Output:
big = 2147483647
bigger = -4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
语言规范要求对溢出计算进行环绕行为,因此它是一个功能。由于大多数溢出在编译时无法检测到,因此检查那些可以检测到的溢出的价值是值得怀疑的,特别是因为它们可能是故意的。
The language specification mandates wrap-around behaviour for overflowing computations, so it's a feature. Since most overflows cannot be detected at compile time, it's of dubious value to check for those that can be detected, in particular because they may have been intentional.
这是在 java 语言规范中指定的: http: //java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.2
此外,几乎所有语言都会通过使用环绕溢出而不是抛出溢出错误来表现这种方式。为什么?部分原因是历史上一直是这样,而且因为如果将整数算术视为纯粹的二进制运算,会更容易。
This is specified in the java language specification: http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.2
Also, almost all languages will behave this way by using wrap-around overflow instead of throwing an overflow error. Why? Partly because historically it has been this way, and because it is easier if you look at integer arithmetic as a purely binary operation.
这可能是由于性能问题。
对于大量数据,验证数值范围的正确性可能会导致速度显着减慢。
It's probably due to performance concerns.
For large amount of data, verifying numeric range correctness might cause a significant slowdown.