From JLS Sec 5.2, "Assignment contexts" (emphasis mine):
In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:
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.
- ...
It's simply not allowed for long
s by the spec.
Note that the second bullet point here says that this happens irrespective of the boxing: assigning a constant long
expression to a byte
variable would similarly fail:
// Both compiler errors.
byte primitive = 0L;
Byte wrapped = 0L;