One, automatic type conversion
In Java programs, it is often necessary to convert data between different basic data types. For example:
,
the int variable score1 in the code can perform the assignment directly for the double type variable score2, and The result is: 82.0
This conversion is called automatic type conversion .
Of course, automatic type conversions are required to meet specific conditions : The target type is greater than the meta type
Ii. Coercion Type conversion
How do you do this when you need to assign the value of a double variable to an int variable?
Obviously, this conversion is not automatic! Because the int type has a smaller storage range than a double type. This is done by forcing type conversions .
Syntax:(data type) values
Operation Result:
As you can see, by forcing the type conversion to assign 75.8 to the int variable, the result is 75, and the number is not rounded, but the decimal bit is truncated directly.
See, forced type conversions can result in loss of data Oh , the small partners in the application must be cautious Oh!
Automatic type conversion and forced type conversion for Java