Example 2
The statements
char c = 'A';
c = c + 1;
will produce an error message becuase the expression c + 1 combines a char and an int result.
The assignment to the char variable c is an attempt to convert an int to a char - a narrowing
conversion. We can correct the problem by writing the assignment statement in the form
c = (char)(c + 1);
The numeric values of the encodings for the characters '0', '1',..., '9' are not 0, 1, ..., 9
but they are sequential values. We can use thus fact to convert between the char form of a
digit and the numerical value of the digit. The next example shows how this can be done.
|
|