You cannot convert from int to char, so this would be illegal
int i = 88; char c = i;
,
However this is allowed char c = 88;
.
Isn\'t
Actually, converting from int
to char
is legal, it just requires an explicit cast because it can potentially lose data:
int i = 88;
char c = (char) i;
However, with the literal, the compiler knows whether it will fit into a char
without losing data and only complains when you use a literal that is too big to fit into a char
:
char c = 70000; // compiler error