Java 6和Java 7之间的自动解包的区别 [英] Differences in auto-unboxing between Java 6 vs Java 7

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

问题描述

我注意到在Java SE 6和Java SE 7之间的自动拆箱行为的差异。我想知道为什么,因为我找不到这两个版本之间的行为的任何更改的文档。



下面是一个简单的例子:

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

这从Java SE 7的javac编译得很好。但是,如果我给编译器-source 1.6参数我在最后一行得到一个错误:

 不可逆类型
发现:java.lang.Object
必需:int



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



那么什么给了?从一些更多的实验看来,Java 6中的解包只能是明确(在编译时)的盒子类型的值。例如,这在两个版本中工作:

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

所以看来,在Java 6和7之间,取消装箱功能得到了增强, unbox对象类型,而不知道(在编译时)值是正确的盒式类型。然而,通过阅读通过Java语言规范或博客帖子,当时写的Java 7出来,我看不到这个东西的任何变化,所以我想知道什么变化,这个功能是什么



只是一个好奇心:由于变化,可能触发错误拆箱:

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

这会编译正常,但在运行时会产生ClassCastException。



希望有人能帮助我参考一下。提前感谢。

解决方案

它看起来像第5.5节Java 7 JLS的铸造转换 Java 5/6 JLS中的同一部分,可能是为了澄清允许的转化。



Java 7 JLS说


引用类型的表达式可能会转换为基本类型没有错误,通过解包转换。


Java 5/6:


引用类型的值可以通过解封装转换(§5.1.8)转换为基本类型。


Java 7 JLS还包含从引用类型到基元的允许转换(该表不包括在Java 5/6 JLS中)的表(表5.1)。这显式地将从对象到原语的转换明确地列为具有解除封装的缩小参考转换。



原因在此电子邮件


底线:如果规格。 allow(Object)(int)它也必须允许(int)(Object)。



Hi 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.

Here's a simple example:

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

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

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.

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];

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];

This compiles fine but gives a ClassCastException at runtime.

Hope someone can help me with a reference on this. Thank you in advance.

解决方案

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 says

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

Java 5/6:

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

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.

The reason is explained in this email:

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

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

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