byte b=9 ; b=b+6 ;
gives compilation error (possible loss of precision ) why does b=9 not give error whereas b=b+9 give loss
b=9
b=b+9
During expression evaluation there is an implicit type casting to int, due to the promotion rules in java
int
So intead of :
byte b=1; b=b+1; //will give error
Write:
b=(byte)b+1;
Here are some links you can refer:
Link 1
Link 2
Link 3