0

Consider the following code:

short x = 1;  

x += 1;    // all good
x = x + 1; // error, since (x + 1) returns int

The question is:

Is it safe to say that internally Java automatically cast x to int, then back to short? Something like this:

x += 1 will be expand to x = (short) ((int) x + 1);

The same question about the rest of the assignment operators: += -= *= /= %= &= ^= |= <<= >>= >>>=

No Name QA
  • 724
  • 1
  • 6
  • 20
  • 2
    From the Specs: *A compound assignment expression of the form `E1 op= E2` is equivalent to `E1 = (T)((E1) op (E2))`, where `T` is the type of `E1`, except that `E1` is evaluated only once.* – Lino Jul 14 '20 at 08:52

1 Answers1

0

In compound assignment operator type-casting is automatically done by compile.

hami
  • 54
  • 2