Java 6 与 Java 7 之间自动拆箱的差异 [英] Differences in auto-unboxing between Java 6 vs Java 7

查看:19
本文介绍了Java 6 与 Java 7 之间自动拆箱的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Java SE 6 和 Java SE 7 之间自动拆箱行为的不同.我想知道为什么会这样,因为我找不到关于这两个版本之间这种行为变化的任何文档.

I have noted a difference in auto unboxing behavior between Java SE 6 and Java SE 7. I'm wondering why that is, because I can't find any documentation of changes in this behavior between these two versions.

这是一个简单的例子:

Object[] objs = new Object[2];
objs[0] = new Integer(5);
int myInt = (int)objs[0];

使用 Java SE 7 中的 javac 可以很好地编译.但是,如果我给编译器提供-source 1.6"参数,我会在最后一行出现错误:

This compiles fine with javac from Java SE 7. However, if I give the compiler the "-source 1.6" argument I get an error on the last line:

inconvertible types
found   : java.lang.Object
required: int

我尝试下载 Java SE 6 以使用本机版本 6 编译器(没有任何 -source 选项)进行编译.它同意并给出与上述相同的错误.

I tried downloading the Java SE 6 to compile with the native version 6 compiler (without any -source option). It agrees and gives the same error as above.

那是什么?从更多的实验来看,Java 6 中的拆箱似乎只能拆箱那些明确(在编译时)属于装箱类型的值.例如,这适用于两个版本:

So what gives? From some more experimentation it seems that the unboxing in Java 6 can only unbox values that clearly (at compile time) is of the boxed type. For instance, this works in both versions:

Integer[] objs = new Integer[2];
objs[0] = new Integer(5);
int myInt = (int)objs[0];

因此,似乎在 Java 6 和 7 之间,拆箱功能得到了增强,因此它可以一次性转换和拆箱对象类型,而无需(在编译时)知道该值是正确的装箱类型.但是,通读 Java 语言规范或在 Java 7 发布时撰写的博客文章,我看不到这件事的任何变化,所以我想知道变化是什么以及这个功能"被称为什么?

So it seems that between Java 6 and 7, the unboxing feature was enhanced so that it could cast and unbox object types in one swoop, without knowing (at compile time) that the value is of the proper boxed type. However, reading through the Java Language Specification or blog postings that were written at the time Java 7 came out, I can't see any change of this thing, so I'm wondering what the change is and what this "feature" is called?

只是好奇:由于更改,可能会触发错误"的拆箱:

Just a curiosity: Due the change, it is possible to trigger "wrong" unboxings:

Object[] objs = new Float[2];
objs[0] = new Float(5);
int myInt = (int)objs[0];

这编译得很好,但在运行时给出了 ClassCastException.

This compiles fine but gives a ClassCastException at runtime.

对此有任何参考吗?

推荐答案

看起来像Java 5/6 JLS 中的同一部分,可能是为了阐明允许的转换.

It looks like the language in section 5.5 Casting Conversion of Java 7 JLS was updated in comparison to the same section in the Java 5/6 JLS, probably to clarify the allowed conversions.

Java 7 JLS 说

Java 7 JLS says

通过拆箱转换,引用类型的表达式可以无误地转换为原始类型.

An expression of a reference type may undergo casting conversion to a primitive type without error, by unboxing conversion.

Java 5/6:

可以通过拆箱转换(第 5.1.8 节)将引用类型的值转换为原始类型.

A value of a reference type can be cast to a primitive type by unboxing conversion (§5.1.8).

Java 7 JLS 还包含一个表(表 5.1),其中包含从引用类型到原语的允许转换(该表不包含在 Java 5/6 JLS 中).这明确列出了从 Object 到基元的强制转换,作为带拆箱的缩小引用转换.

The Java 7 JLS also contains a table (table 5.1) of allowed conversions (this table is not included in the Java 5/6 JLS) from reference types to primitives. This explicitly lists casts from Object to primitives as a narrowing reference conversion with unboxing.

原因在这封电子邮件中说明:

底线:如果规范.允许 (Object)(int) 它也必须允许 (int)(Object).

Bottom line: If the spec. allows (Object)(int) it must also be allowing (int)(Object).

这篇关于Java 6 与 Java 7 之间自动拆箱的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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