这是JVM错误还是“预期行为"? [英] Is this a JVM bug or "expected behavior"?

查看:90
本文介绍了这是JVM错误还是“预期行为"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到了一些意外的行为(相对于我的个人期望而言是意外的),我想知道是否是JVM中存在错误,或者这可能是我不了解某些细节的附带情况到底应该发生什么.假设我们自己在main方法中有以下代码:

I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't understand some of the details of what exactly is supposed to happen. Suppose we had the following code in a main method by itself:

int i;
int count = 0;
for(i=0; i < Integer.MAX_VALUE; i+=2){
  count++;
}
System.out.println(i++);

幼稚的期望是它将打印Integer.MAX_VALUE-1,最大的甚至可表示的int.但是,我相信整数算术应该在Java中翻转",因此在Integer.MAX_VALUE中加1应该会导致Integer.MIN_VALUE.由于Integer.MIN_VALUE仍然小于Integer.MAX_VALUE,因此循环将循环遍历负整数.最终它将恢复为0,并且此过程应作为无限循环重复进行.

A naive expectation would be that this would print Integer.MAX_VALUE-1, the largest even representable int. However, I believe integer arithmetic is supposed to "rollover" in Java, so adding 1 to Integer.MAX_VALUE should result in Integer.MIN_VALUE. Since Integer.MIN_VALUE is still less than Integer.MAX_VALUE, the loop would keep iterating through the negative even ints. Eventually it would get back to 0, and this process should repeat as an infinite loop.

当我实际运行此代码时,会得到不确定的结果.打印的结果往往大约为一百万,但是确切的值却有所不同.因此,当我认为循环应该是无限循环时,循环不仅终止了,而且似乎随机终止了.发生了什么事?

When I actually run this code, I get non-deterministic results. The result that gets printed tends to be on the order of half a million, but the exact value varies. So not only is the loop terminating when I believe it should be an infinite loop, but it seems to terminate randomly. What's going on?

我的猜测是,这要么是JVM中的错误,要么是正在进行许多时髦的优化,从而产生了预期的行为.是哪一个?

My guess is that this is either a bug in the JVM, or there is a lot of funky optimization going on that makes this expected behavior. Which is it?

推荐答案

已知错误.与

http://bugs.sun.com/bugdatabase/view_bug.do? bug_id = 6196102

http://bugs.sun.com/bugdatabase/view_bug.do? bug_id = 6357214

和其他人.

我认为它们被认为是低优先级的,因为它们不在现实世界中出现.

I think they're considered low-priority to fix because they don't come up in the real world.

这篇关于这是JVM错误还是“预期行为"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆