Can assign integer value to char but can't assign integer variable to char
Solution 1:
A char
can be assigned to an int
without a cast because that is a widening conversion. To do the reverse, an int
to a char
requires a cast because it is a narrowing conversion.
See also JLS. Chapter 5. Conversions and Promotions.
Solution 2:
His question is why his code does not compile, not how to do what he's trying to do.
The reason the line
char c = n
does not compile, is because the range of char (-2^15 to 2^15 - 1) is much smaller than the range of int (-2^31 to 2^31 - 1). The compiler sees you are trying to assign an int to a char, and stops you, because it realizes this.