For instance, I want to do something like the following:
char output = 'A' + 1;
getting output of 'B'
However, simply trying what I have above gives a casting error.
Yes, the binary +
operator isn't defined on char
, so you end up with implicit conversions to int
. You can use a pre/post-increment operator:
char x = 'A';
x++;
... because the ++
operator is defined for char
.
But otherwise, you need a cast:
char x = 'A';
x = (char) (x + 1);
See more on this question at Stackoverflow