java怪异的赋值规则

Vin*_*uan 7 java integer char variable-assignment

short s = 'a';       // valid
Short ss = 'a';      // valid
int i = 'a';         // valid
Integer ii = 'a';    // invalid
Run Code Online (Sandbox Code Playgroud)

为什么整数ii ='a'无效,但int i ='a'有效?为什么短ss ='a'有效,但整数ii ='a'无效?


另一个问题:

byte b;
final short s = 1;
final Short ss = 1;
final int i =1;
final Integer ii = i;
final long L = 1;
final Long LL =1L;

b = s;     // valid
b = ss;    // invalid
b = i;     // valid
b = ii;    // invalid
b = L;     // invalid
b = LL;    // invalid  
Run Code Online (Sandbox Code Playgroud)

为什么b = L; 无效,而b = s; 有效吗?

请不要说这一切都是因为JLS这么说.我想知道为什么JLS有这些不一致和非直观的规则.我错过了什么?

Ole*_*hov 5

所以,行:

Short s = 'a'; // is valid ...
Run Code Online (Sandbox Code Playgroud)

因为char是无符号的16位值(最大值是65,536)而short是有符号的16位值(最大值是32,767),所以有一个缩小的原始转换(char to short),然后是一个装箱转换(short to short) ).

short s = 'a'; // is valid - this is a narrowing primitive conversion (char -> short)
Run Code Online (Sandbox Code Playgroud)

这些是特殊情况:

另外,如果表达式是byte,short,charint类型的常量表达式:

  • 如果变量的类型是byte,shortchar,则可以使用缩小的基元转换,并且常量表达式的值可以在变量的类型中表示.

如果变量的类型是:则可以使用缩小的基元转换,然后进行装箱转换:

  • 字节和常量表达式的值可在类型字节中表示.

  • Short和常量表达式的值可以在short类型中表示.

  • 字符和常量表达式的值可在char类型中表示.

我们来看下一个例子:

Integer ii = 'a'; // is invalid - not a special case according to Oracle docs
int i = 'a';      // is valid - widening primitive conversion (char -> int) is allowed
Run Code Online (Sandbox Code Playgroud)

还有一个案例来自你的问题:

byte b;
final long L = 1;
b = L // error - incompatible types
Run Code Online (Sandbox Code Playgroud)

为什么线b = L无效?因为它不是上面描述的特殊情况,我们可以在演员表中丢失信息,这就是你必须明确地执行它的原因:

b = (byte) L; // is valid - narrowing primitive conversion (long -> byte)
Run Code Online (Sandbox Code Playgroud)

另外,看看一个非常有用的.

在JLS文档中有很多关于所有这些规则的信息,您无需担心所有这些规则.关于你的上一个问题,我能说的是,如果没有隐式缩小转换,任何整数文字在下一种情况下都需要强制转换:

// Cast is permitted, but not required - profit!
byte  b = (byte)  100;
short s = (short) 100;
Run Code Online (Sandbox Code Playgroud)

多亏了我们可以将它改为:

byte  b = 100;
short s = 100;
Run Code Online (Sandbox Code Playgroud)