問題描述
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
,那么將 Character
分配給一個 char
,或者 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
、浮點數
,或雙數
.
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
,則不允許緊跟裝箱,并且不能從 提升非常量表達式byte
到 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!