Hi Zhou,
Answer to your first code segment,
1: byte b=1;
b + = 1; // line 1
Java compiler reads the statement at line 1 as,
b = (byte)(b+1);
or in more general, Java reads,
var1 op = var2; // op is any binary operator NOT unary!!
as,
var1 = (type of var1)(var1 op var2);
As you have seen Java do this casting by it's self i.e "implicit cast".
Now answer to your second question.
Always remember that calculation in Java performs in "at least" 32 bits.Suppose,
byte b=1;
byte b= b+1; //line 1
In this case, at line 1 Java compiler will upgrade b(i.e of 8 bits) into int i.e 32 bits & then perform calculation.So, the result of calculation will also in 32 bits.And you are at line 1 trying to assing the 32 bit result in byte which is of 8 bits.So,
"explicit cast" is needed in this situatuion in order to remove compile time error.That is,
b = (byte)(b + 1);
Hope now it is clear to you,
After clearing this confusion try this code & suggest what happens..
long la = 7;
short sa = 3;
int ia = la + sa ;
Regards,
Hassan.