The package Basetype;/*java automatic type promotion and narrowing conversion * * 1. When a variable of type char,short,byte is assigned to a number that exceeds the maximum range that he can represent, Java automatically converts that number to int. * * 2. When the variables of the Char,short,byte type are arithmetic, the bitwise operations are automatically promoted to the int type * But the arithmetic operation, the bitwise operation operation and their compound operation are different, the code is the following. * Arithmetic operators include: +,-, *,/,%,+ (unary operator),-(unary operator) * Bitwise operator operators include shift operations (&,^,|), bitwise operations (<<,>>,>>>) * */public Class Autoconversion {public static void main (String args[]) {//because the int type is assigned to a type that is smaller than the range he represents, the compiler type check will give an error, prompting for a forced type conversion, which is the narrowing conversion byte a = 129; Because byte is 8 bytes, the range represented is from 128 to +127, so the assignment 128 is automatically converted to int type, the compiler will error the type check, it must be narrowed to bytebyte a = (byte) 129;} public void Bytetest (byte x, byte y) {x = (byte) (x*y);//Because the result of X*y is automatically promoted to int, the narrowing conversion must be used when the value is re-assigned to X, and the other arithmetic operators and bitwise operations are the same x = ( BYTE) x = (byte) (x+y); x = (Byte) (x-y); × = (Byte) (x%y); x--; x = (byte) +y; x = (byte)-y; x = (byte) ~y; x = (byte) (x&y); x = (byte) (x|y); x = (byte) (x^y); x = (byte) (x>>y); x = (byte) (x<<y); x = (byte) (X> ; >>y); x + = y;//The composite operation is different, although the type promotion is not required, but the result is the same as the result of using arithmetic operations or bit operations,But the specific why does not need, the book also did not say that the future in contact with the supplementary x-= y; x *= y; x/= y; x%= y; x <<= 1; x >>= 1; x >>>= 1; x &= y; x |= y; x ^= y;}}
"Thingking in Java" notes
Automatic type conversion for Java