how does shorthand operator in java works different from normal operator? -


this question has answer here:

i know default numbers stored integer in java but

byte x = 10; x = x + 10; 

is giving error while

byte x = 10; x += 10; 

is compiling fine

jls have answer

a compound assignment expression of form e1 op= e2 equivalent e1 = (t) ((e1) op (e2)), t type of e1, except e1 evaluated once.

short x = 3; x += 4.6; 

and results in x having value 7 because equivalent to:

short x = 3; x = (short)(x + 4.6); 

so in case second statement equlas to

x = (byte) x + 10; 

that reason compiler happy about.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -