Dear sir in the above code at line 3 that c=(byte)(a+b) here (byte)it is ok but why there is need to also enclose a+b in ()although at line 6 it is working fine and if i m making like c=(byte)a+b a compile time error that possible loss of precision found int requered byte although it is working at line 6
When arithmetic operations are performed on byte variables as in the above case, they are first promoted to integers and then the operation takes place. Therefore, you'll need to cast the complete result if it is stored in a byte. If you use byte c = (byte)a + b; You are only casting a and not the complete result i.e. a + b. Hence, the compilation error.
In the above case, it doesnt matter if the result is casted as the variable c is float and thus can accomodate the result of the addition operation.