widening rules

 

■ Here are the widening or promotion rules for operands:

 

(1) If either operand is a double, the other is made a double, otherwise

(2) If either operand is a float, the other is made a float, otherwise

(3) If either operand is a long, the other is made a long, otherwise

(4) Both are just made int.

 

■ Because of automatic widening promotion to 32-bit int and even long:

 

(1) You must always explicitly cast any results shorter than int. 

 

        short sh = (short) (shortvariable1 & shortvariable2);

 

 (2) You must use complete enclosing parens to avoid compiler loss-of-precision errors. 

 

        char c3 = (char) (c1 & c2);               This works. 

        char c3 = (char) c1 & c2;                 This will not compile.