It's wrong because char
is smaller than int
. In c1 + 1
the c1
is promoted to an
int
to be added to 1
. When you try to put it back in a char
java complains
because it can't promise that an int
will fit in a char
-edit-
In the case of char c = 123
the 123
part is known, so java can really promise that
it will always fit in a char
. This will work as well:
final char c0 = 123;
char c1 = c0 + 1;
and this:
final int i0 = 123;
char c1 = i0 + 1;