1. Define two byte data types A, B and C, and give them values of 1 and 2 and a+b respectively. After adding the formula a+b=3, the result "3" will be judged as plastic data by the compilation environment. If the plastic data is assigned to C, the system will report an error, so the format "Target data type? Variable = (target data type) cast statement of data.
2. According to the format of cast statement, it is easy to get "byte c = (byte)(a+b)". ;
3. In this way, the "3" of plastic data is assigned to C of byte data type, in which the forced type conversion of data is completed.
Extended data:
Basic type conversion principle:
1, type conversion mainly in? Assignment, method call, arithmetic operation? It happens in three situations.
A, assignment and method call conversion rules: automatic conversion from low-order types to high-order types; Forced type conversion is required from high-order type to low-order type:
(1) Boolean and other basic data types cannot be converted to each other; ?
(2) byte types can be converted into short, int, long, float and double;; ?
(3)short can be converted into int, long, float and double;; ?
(4)char can be converted into int, long, float and double;; ?
(5)int can be converted into long, float and double;; ?
(6)long can be converted into float and double;; ?
(7)float can be converted into double;; ?
B, type conversion in arithmetic operation:
1, which is basically converted into a high-order data type first, and then participates in the operation, and the result is also a high-order data type;
2. Byte short character operation will be converted into int;
(1) If one of the operands is double, the other operand is converted to double first, and then participates in the arithmetic operation. ?
(2) If neither operand is a double, when one operand is a float, the other operand is converted to a float first, and then participates in the operation. ?
(3) If neither operand is a double or a float, if one operand is a long, then the other operand is converted into a long first, and then participates in the arithmetic operation. ?
(4) If the two operands are not double, float or long, first convert the two operands into int, and then participate in the operation.
Special:?
(1) If abbreviation operators such as+= and * = are used, the system will automatically convert the operation result to the type of the target variable.
(2) ? When the operator is an automatic increment operator (++) or an automatic decrement operator (-), if the operand is byte, the type of short or char will not change;
References:
Baidu encyclopedia -java keywords