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= e2 equivalent e1 = (t)((e1) op (e2)), t type of e1, except e1 evaluated 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

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -