I can’t remember where I saw this code but I don't remember it giving me a reason why something didn’t compile.
I understand that when we use arithmetic operators in Java, it will promote short data types, like byte, short & char to an int first then do the operation.
And we need to use explicit casting when going a larger data type to a smaller data type.
With the code below, we assign the byte and add a and b, why doesn’t c compile?
As in, why do we need parentheses around the a + b?
On line 3 variable a is first cast to byte and then the add takes place. Since the result of adding a and b is promoted to int this will not compile.
At line 4 you first add a and b and the result of this is then cast to a byte.