According to the Java Spec,
The type of an additive expression on numeric operands is the promoted type of its operands.
whereas
the type of the prefix and postfix increment expression is the type of the variable... Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored.
(emphasis added)
So using +
first applies a Widening Primitive Conversion to the operands, turning them into integers, and then maintains that promoted type as the expression's result. ++
also applies promotion, but it also adds a narrowing step afterward so that the result type is the same as the original variable's type.
This behavior makes sense, because you know that a ++
is changing the value of the original parameter, whereas the result of a +
operation could be saved into some other value:
int i = Character.MAX_VALUE + 1;
As the other answers point out, if you want to save the result of some addition back into a character variable, the integer result of a +
operation will need to be cast back down to a char
, like this:
ch1 = (char)(ch1+1)