casting - Java's +=, -=, *=, /= compound assignment operators -
until today, thought example:
i += j; is shortcut for:
i = + j; but if try this:
int = 5; long j = 8; then i = + j; not compile i += j; compile fine.
does mean in fact i += j; shortcut i = (type of i) (i + j)?
as these questions, jls holds answer. in case §15.26.2 compound assignment operators. extract:
a compound assignment expression of form
e1 op= e2equivalente1 = (t)((e1) op (e2)),ttype ofe1, excepte1evaluated once.
an example cited §15.26.2
[...] following code correct:
short x = 3; x += 4.6;and results in x having value 7 because equivalent to:
short x = 3; x = (short)(x + 4.6);
in other words, assumption correct.
Comments
Post a Comment