Weird subtraction

exercise No. 225

Q:

Consider the following code snippet:

Code Result
char c = 0;
c -= 1;
int cc = c;
System.out.println("cc == " + cc);
cc == 65535

How comes the operator c -= 1 acting on c starting from 0 yields a positive value of 65535?

A:

A char ranges from 0 to 2 16 - 1 . The -= operator steps from the lowest possible value to the type's maximum in a cyclic fashion:

    0   00000000 00000000
   -1  -00000000 00000001
-----  ------------------
65535   11111111 11111111