2

In below program we are able perform operation ch1++ but not able to perform ch1=ch1+1

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    char ch1 = 88;
    System.out.println("Ch1 is " + ch1);

    ch1=ch1+1;
    ch1++;
    System.out.println("ch1 is "+ch1);
}
Akash Chavan
  • 325
  • 2
  • 7
  • 22
  • There's a similar question _somewhere_ on SO, but I can't find it. :) The upshot is that two things are happening here. First is that `ch1 + 1` is an int type (not char) and ints can't be implicitly cast to chars -- you have to provide a char. The second is that `x++` isn't actually equivalent to `x = x + 1`, but rather `x = (T) (x + 1)` where T is x's type. In other words, `ch1++` adds the cast that `ch1 + 1` does not, and which is needed. – yshavit Apr 20 '17 at 19:29
  • use this ch1=(char) (ch1+1); – opensam Apr 20 '17 at 19:32
  • thanks for the explanation – Akash Chavan Apr 20 '17 at 19:32
  • This may provide some insight: [Java's +=, -=, *=, /= compound assignment operators](http://stackoverflow.com/questions/8710619/javas-compound-assignment-operators). A similar process is used with pre/post increment. – Obicere Apr 20 '17 at 19:32

3 Answers3

2

ch1+1 has type integer, you need to cast it to char, like that:

(char)(ch1+1)

in more details:

java see two values, one type char, one type integer (1 is integer), to perform + operation java needs both arguments be the same, the safest way is to promote char to int, this is why result is int

ch1++ is different one, java treats this construct as:

ch1 = (char)(ch1+1)

so, cast is added automatically

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
1

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)
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

You need to cast ch + 1 to a (char), as the result of 1 + ch1 is an int

ch1=(char)ch1+1;
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • yes i can do it that way but my question is isnt ch1++ and ch1=ch1+1 both same ?? – Akash Chavan Apr 20 '17 at 19:29
  • @AkashChavan technically, there is an implicit cast on the pre/post increments. Same with doing `ch1 += 1`. See: [Java's +=, -=, *=, /= compound assignment operators](http://stackoverflow.com/questions/8710619/javas-compound-assignment-operators) – Obicere Apr 20 '17 at 19:31
  • `ch1++` and `ch1+1` are not the same because 1 is an `int`, and an integer plus a char is a (4 byte) `int`, as opposed to a 2 byte `char` – ControlAltDel Apr 20 '17 at 19:31