Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles:
byte b = 0;
++b; b++; --b; b--;
b += b -= b *= b /= b %= b;
b <<= b >>= b >>>= b;
b |= b &= b ^= b;
And thanks to auto-boxing and auto-unboxing, the following also compiles:
Integer ii = 0;
++ii; ii++; --ii; ii--;
ii += ii -= ii *= ii /= ii %= ii;
ii <<= ii >>= ii >>>= ii;
ii |= ii &= ii ^= ii;
And yet, the last line in the following snippet gives compile-time error:
Byte bb = 0;
++bb; bb++; --bb; bb--; // ... okay so far!
bb += bb; // DOESN'T COMPILE!!!
// "The operator += is undefined for the argument type(s) Byte, byte"
Can anyone help me figure out what's going on here? The byte b
version compiles just fine, so shouldn't Byte bb
just follow suit and do the appropriate boxing and unboxing as necessary to accommodate?
Extra question
So is there a way to make compound assignment operators work with Byte
, Character
, and Short
on the left hand side, or are they simply illegal(!!!) for these types?
int
tobyte
, and there'sbyte
toByte
, the compiler would figure out that there's a valid path fromint
toByte
after all! Apparently this is not the case? Is there a way to make the compound assignment work with an explicit cast perhaps? – Filiation