The difference is that in the ++ operator there is implicit casting from int to byte , whereas you have to do it explicitly in case you use b = b + 1
b = b + 1; // Will not compile. Cannot cast from int to byte
You need an explicit cast:
b = (byte) (b + 1);
Whereas b++ will work fine. The ++ operator automatically casts the value b + 1 , which is an int byte .
This is clearly indicated in JLS - ยง15.26.2 Complex assignment operators :
A compound assignment expression in the form E1 op = E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is a type of E1, except that E1 is evaluated only once
Note that the operation b + 1 will give you an int type result. That is why you need an explicit cast in the second assignment.
Rohit jain
source share