Possible Duplicate:
Java += operator
We can add a value into any variable either b+=8
or b=b+8
both will return the value adding 8
into the variable b
. I got the question in my interview, it was
byte b=7;
b=b+8; //compile error
What would be output, I ticked compile time error
, since adding byte
and int
will be int
(I believe) and since, we are trying to store int
value into byte
. But, when I tried below code myself
byte b=7;
b+=8; //OK
Then, the above code compiles and run perfectly without any error and return 15
. Now, my question is why and how ? I mean, why it doesn't requires explicit casting ?