JDK编译错误
在有人对 JDK 停产大喊大叫之前,我想指出我的问题不是关于如何编译以下内容。这里有一个真正的问题,这不是关于 JDK 1.5 被 EOL'ed...
JDK 1.5 下的以下内容,直到 1.5.0_22(我能找到的最后一个)在我的系统上产生编译器错误:
private Object[] boozinga() {
boolean b = Math.abs(42) > 0;
Object[] res = new Object[1];
res[0] = b ? new int[1] : new String[1];
return res;
}
更改 数学.abs(42) > 0 到 true 允许编译。
将三元“赋值”更改为 if/else 允许编译。
使用 JDK 1.6 允许编译。
所以我想知道:上面的代码中是否有一些东西在Java 1.5下是不合法的,而在Java 1.6下是允许的?
对于那些使用 Java 1.5 的人来说,它也会崩溃吗?
崩溃说的是这样的:
发生异常 编译器(1.5.0_22)。请提交错误 在 Java 开发人员连接处 (http://java.sun.com/webapps/bugreport) 检查 Bug Parade 后 重复。包括您的程序和 您的以下诊断 报告。谢谢。
我认为为已停产的 JDK 填写错误报告是徒劳的,但我仍然想知道上面的内容是否是有效的 Java 1.5 代码。
Before anyone screams about EOL'ed JDK, I'd like to point out that my question is not about how to compile the following. There is a real question here and it's not about JDK 1.5 being EOL'ed...
The following under JDK 1.5, up to 1.5.0_22 (the last one I could find) produces on my system a compiler error:
private Object[] boozinga() {
boolean b = Math.abs(42) > 0;
Object[] res = new Object[1];
res[0] = b ? new int[1] : new String[1];
return res;
}
Changing the Math.abs(42) > 0 to true allows compilation.
Changing the ternary "assignment" to an if/else allows compilation.
Using JDK 1.6 allows compilation.
So I was wondering: is there something not legal in the above code under Java 1.5 and that is allowed under Java 1.6?
Does it crash for those of you that are under Java 1.5 too?
The crash says something like this:
An exception has occured in the
compiler (1.5.0_22). Please file a bug
at the Java Developer Connection
(http://java.sun.com/webapps/bugreport)
after checking the Bug Parade for
duplicates. Include your program and
the following diagnostic in your
report. Thank you.
I take it filling a bug report for an EOL'ed JDK is an exercice in futility but still, I'd still like to know if the above is valid Java 1.5 code or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是合法的。证据是 JDK 1.6.0_21 使用选项
-source 1.5 -target 1.5
编译它。不能使用带有这些选项的 JDK 1.6 来编译并使用 JRE 1.5 来运行吗?它对我来说也崩溃了(JDK 1.5.0_12)。即使这样,它也会崩溃:
编译器的困难在于
b 的类型? new int[1] : new String[1]
是 java.lang.Object & java.io.Serialized & java.lang.Cloneable。I think it is legal. The evidence is that JDK 1.6.0_21 compiles it with options
-source 1.5 -target 1.5
. Can't you use JDK 1.6 with these options to compile and JRE 1.5 to run?It crashes for me, too (JDK 1.5.0_12). It crashes for me even with:
The difficulty for the compiler is that the type of
b ? new int[1] : new String[1]
isjava.lang.Object & java.io.Serializable & java.lang.Cloneable
.这里的问题是编译器无法确定表达式
b 的类型?新 int[1] :新 String[1]
。我之前有过类似的情况(我认为是 1.1.8 或 1.2 - 但有真正的错误消息,而不是编译器崩溃),然后简单地使用强制转换来帮助编译器。我没有查看语言规范对此的说明 - 但编译器永远不应该因异常而崩溃,它应该给出真正的错误消息。
The problem here is that the compiler has trouble to decide the type of the expression
b ? new int[1] : new String[1]
. I had something like this before (with 1.1.8 or 1.2, I think - but with a real error message, not a compiler crash), and then simply used a cast to help the compiler here.I didn't look what the language specification says about this - but the compiler should never crash with an exception, it should give a real error message.