为什么这些代码在Java 7而不是Java 8中有效? [英] Why does this bit of code work in Java 7 and not Java 8?

查看:139
本文介绍了为什么这些代码在Java 7而不是Java 8中有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用IDE Eclipse版本:Neon.2发行版(4.6.2)和版本java版本8更新131.
在此代码中,IDE出错 - 类型不匹配:无法转换从byte到Integer:

I'm currently using IDE Eclipse Version: Neon.2 Release (4.6.2) and version java Version 8 Update 131. In this code, the IDE gives an error - "Type mismatch: can not convert from byte to Integer":

Integer i = (byte) 10;

但是这个代码在IDE Eclipse版本中正确执行:Indigo Service Release 2和java Version 7。在第8版java的扩展转换机制中实质上已经改变了,因为我不认为它与IDE版本有关?

But this code is correctly executed in IDE Eclipse Version: Indigo Service Release 2 and java Version 7. What essentially changed in the widening conversion mechanism in the 8th version of java, because I do not think it is related to the IDE version?

推荐答案

如果一次进行两次转换,您的作业将有效

Your assignment would only work, if two conversions were made at once


  1. 字节扩展 int

  2. int 装箱到整数

  1. Widening from byte to int
  2. Boxing from int to Integer

显然,较旧的Eclipse版本允许这样做,已经更改,而 javac 从未允许这个(我测试过Java 6到Java 9)。

Apparently, older Eclipse versions allowed this, which has been changed, whereas javac never allowed this (I tested Java 6 to Java 9).

要找出,正确的行为是什么,我们请参考规范

To find out, what is the correct behavior, we have do refer to the specification:


5.2。赋值上下文



赋值上下文允许将表达式的值赋值(第15.26节)给变量;必须将表达式的类型转换为变量的类型。

5.2. Assignment Contexts

Assignment contexts allow the value of an expression to be assigned (§15.26) to a variable; the type of the expression must be converted to the type of the variable.

分配上下文允许使用以下之一:

Assignment contexts allow the use of one of the following:


  • 身份转换(§5.1.1)

  • 扩展原始转换(§5.1.2)

  • 扩大参考转换(§5.1.5)

  • 一个拳击转换(§5.1.7),可选地后跟一个加宽的参考转换

  • 一个拆箱转换(第5.1.8节),可选地后跟一个加宽的原语转换。

  • an identity conversion (§5.1.1)
  • a widening primitive conversion (§5.1.2)
  • a widening reference conversion (§5.1.5)
  • a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
  • an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

请注意,我们需要的代码组合不在列表中。我们可以从该列表中得出以下结果:

Note that the combination we would need for the code is not within the list. We can derive from that list, that the following should work:

Number i = (byte) 10;

这是从 byte 到装箱的转换字节,然后扩大参考转换为数字

which is a boxing conversion from byte to Byte, followed by a widening reference conversion to Number.

Byte b = 42;
int i = b;

这是从字节到目标的拆箱转换 byte ,然后是从 byte int 。

which is an unboxing conversion from Byte to byte, followed by a widening primitive conversion from byte to int.

由于(字节)10 是编译时常量,我们还要考虑

Since (byte) 10 is a compile-time constant, we also have to consider


此外,如果表达式是 byte 类型的常量表达式(第15.28节), char int

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:


  • 如果变量的类型是 byte ,<$ c,则可以使用缩小的原始转换$ c> short char ,并且常量表达式的值可以在变量类型中表示。

  • 如果变量的类型是:


    • Byte,则可以使用缩小的原始转换,然后进行装箱转换。 ,常量表达式的值可在 b类型中表示yte

    • ,常量表达式的值可在<$ c $类型中表示c>短。

    • 字符,常量表达式的值可在<$类型中表示c $ c> char 。

    • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
    • A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
      • Byte and the value of the constant expression is representable in the type byte.
      • Short and the value of the constant expression is representable in the type short.
      • Character and the value of the constant expression is representable in the type char.


      $来自同一部分的b $ b

      from the same section.

      这允许一些有趣的组合,例如

      This allows some interesting combinations like

      Character c = (byte)10;
      

      Byte b = 'x';
      

      但没有变量类型整数

      如上所述,从字节转换为 Integer 不在列表中,因此较旧的Eclipse版本允许它的事实可以被认为是在较新版本中修复的错误。

      As said, the conversion from byte to Integer is not within the list, so the fact that older Eclipse versions allowed it, can be considered a bug that has been fixed in the newer version.

      这篇关于为什么这些代码在Java 7而不是Java 8中有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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