There are two ways to perform conversion: explicit coercion and implicit coercion.
1, explicit conversion
The format is: int b = (int) a; ;
After the cast operator is operated, it returns a numeric value of type int. This cast operation will not change the operand itself, and the operand itself will not change after the operation.
2. Implicit cast
Implicit type conversion occurs in assignment expressions and function call expressions with return values. In an assignment expression, if the types of operands on the left and right sides of the assignment operator are different, the operands on the right side of the assignment operator are forcibly converted to the type values on the left side of the assignment operator, and then assigned to the variables on the left side of the assignment operator.
When calling a function, if the type of the returned expression is different from the type of the function return value, the value of the returned expression is forcibly converted to the type of the function return value, and then the return value is returned. For example:
int n; Double d = 3.78n = d;;
After execution, the value of n is 3, while the value of d is still 3.78.
Extended data:
Rules followed by automatic types:
1. If the operands involved are of different types, first convert them to the same type, and then perform the operation.
2. The conversion is carried out in the direction of increasing the data length to ensure that the accuracy is not reduced. Such as int and long operations, the int quantity is converted into long before the operation.
3. All floating-point operations are performed with double precision. Even expressions that only contain floating-point single-precision operations should be converted to double-precision types before operations.
4. When 4.char type and short type participate in the operation, they must be converted into int type first.
5. In the assignment operation, when the data types of the quantities on both sides of the assignment number are different, the type of the quantity on the right side of the assignment number will be converted into the type of the quantity on the left. If the data type length of the right quantity is longer than that of the left quantity, some data will be lost, the accuracy will be reduced, and the lost part will be discarded directly.
Baidu Encyclopedia-Forced Type Conversion