Java允许将字节分配给java.lang.Short,但不允许分配给java.lang.Integer [英] Java allows to assign byte to java.lang.Short but not to java.lang.Integer

查看:61
本文介绍了Java允许将字节分配给java.lang.Short,但不允许分配给java.lang.Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

final byte b = 12;  
Short s = b;  
Integer i = b;

对于Short,程序可以很好地进行编译,但对于Integer,则编译失败,并显示不兼容类型"消息.

Program compiles fine for Short but for Integer compilation fails with "incompatible types" message.

我很难理解这种行为.对于这种特定情况,我找不到任何东西.

I am having difficult time trying to understand this behavior. I could not find anything for this specific scenario..

推荐答案

我试图用更广泛的分配上下文来复制它:

I attempted to duplicate this with a wider group of assignment contexts:

final byte b = 12;
Byte b2 = b;
Character c = b;  // Only an error if b isn't final
char c2 = b;      // Only an error if b isn't final
Short s = b;      // Only an error if b isn't final
short s2 = b;
Integer i = b;  // Error, as indicated in the question
int i2 = b;
Long l = b;     // Also an error
long l2 = b;
Float f = b;    // Also an error
float f2 = b;
Double d = b;   // Also an error
double d2 = b;

不仅分配给 Integer ,而且分配给 Float Long Double 也是一个错误.

Assigning not just to a Integer, but also to a Float, a Long or a Double is also an error.

有趣的是,如果 b 的原始声明不是 final ,则将 char Character >,或者 Short 也失败.

Interestingly, if the original declaration of b was NOT final, then assigning to a Character, a char, or a Short fails also.

JLS的5.2节简要说明了分配上下文及其允许的转换的主题.

Section 5.2 of the JLS sheds a little light on the subject of assignment contexts and their allowed conversions.

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

Assignment contexts allow the use of one of the following:

  • 身份转换(第5.1.1节)

  • an identity conversion (§5.1.1)

不断扩展的原始转换(第5.1.2节)

a widening primitive conversion (§5.1.2)

扩展的参考转换(第5.1.5节)

a widening reference conversion (§5.1.5)

装箱转换(第5.1.7节)(可选),然后进行扩大的参考转换

a boxing conversion (§5.1.7) optionally followed by a widening reference conversion

取消装箱转换(第5.1.8节),然后可以选择扩展原始转换.

an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

这涵盖了到更宽泛的原始变量的所有转换,无论 b 是否为 final ,都始终允许.(除非 b 为负,否则这种情况将保持不变,在这种情况下,分配给未签名的 char (或 Character )将失败.)继续:

This covers all of the conversions to wider primitive variables, which are always allowed, whether b is final or not. (That holds unless b is negative, in which case the assignment to an unsigned char (or Character) would fail.) Continuing:

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

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

  • 如果变量的类型为byte,short或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.

如果变量的类型为:

  • 字节,常量表达式的值可以用字节类型表示.

  • Byte and the value of the constant expression is representable in the type byte.

Short,常量表达式的值可以表示为short类型.

Short and the value of the constant expression is representable in the type short.

字符,常量表达式的值可以用char类型表示.

Character and the value of the constant expression is representable in the type char.

因为 b final ,所以表达式 b 恒定表达式,因此可以将其缩小为将 int 常量表达式 12 转换为 byte char short ,然后将其装箱设置为 Byte Character Short ,但奇怪的是,没有设置为 Integer 或任何上方".我能想到的唯一可能的解释是,不允许将要进行原始范围缩小转换的常量表达式专门转换为 Integer Long 浮动 Double .

Because b is final, the expression b is a constant expression, allowing it to be narrowed from the int constant expression 12 to byte, char, or short and then boxed to Byte, Character, or Short, but strangely, not to Integer or anything "above". The only possible explanation I can think of is that constant expressions that are subject to a primitive narrowing conversion aren't specifically allowed to be converted to Integer, Long, Float, or Double.

如果 b 不是 final ,则不允许先进行缩小再进行装箱,并且不能从提升非恒定表达式字节转换为 char .

If b isn't final, then the narrowing followed by boxing isn't allowed, and a non-constant expression can't be promoted from byte to char either.

这篇关于Java允许将字节分配给java.lang.Short,但不允许分配给java.lang.Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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