Integer arithmetic in Java with char and integer literal
It is because the compiler can check that it ('a' + 10
) is within the bounds of a char whereas it cannot (in general) check that 'a' + <an integer>
is within the bounds.
'a' + 10
is a compile-time constant expression with the value of 'k'
, which can initialise a variable of type char
. This is the same as being able to assign a byte
variable with a literal integer in [-128, 127]. A byte
in the range of [128, 255] may be more annoying.