3

I was solving a problem in leet code and noticed the following code was not allowed in Java,

char c = 's';
c = c^c;

While the following was

char c = 's';
c^=c;

Is there a particular reason? Thanks you.

Ayman Elmubark
  • 959
  • 1
  • 6
  • 11

1 Answers1

3

This is also true for plus or minus. c^c is evaluated as an int so the right hand side is int and can not be assigned into a char. In the ^= case, the right hand side is char, and can be applied onto a char. This is not the most obvious behavior.

yoah
  • 7,180
  • 2
  • 30
  • 30