May 2, 2015 51 just after a day, the computer broke down. Not happy, there is no update
type conversions in Java
Automatic type
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 :
1. The target type can be compatible with the source type, such as type double compatible int, but char type cannot be compatible with int
2. The target type is greater than the source type, such as the double type is 8 bytes long, the int type is 4 bytes, so the double type of the variable can directly hold the int type of data, but the reverse is not possible
Forced type conversions in Java
For example, how do you do this when you need to assign the value of a double variable to a variable of type int?
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!
Java starts from zero for the third day