Source: http://www.imooc.com/code/1241
I believe that the small partners have found that, although automatic type conversion is very convenient, but does not meet all the programming needs.
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
Run Result: You can see that by forcing the type conversion to assign 75.8 to the int variable, the result is 75, the value 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!
Task
Practice time again, together to do a blank question!
In the editor, two variables are defined, fill in the Incomplete part of Line 4, and cast the average height to an integer value.
Operation Result:
1 Public classhelloworld{2 Public Static voidMain (string[] args) {3 Doubleheightavg1=176.2;4 intHeightavg2=5 System.out.println (HEIGHTAVG1);6 System.out.println (HEIGHTAVG2);7 }8}
--------------------------------------------------------------------------------------------------------------- -----------
1 Public classhelloworld{2 Public Static voidMain (string[] args) {3 Doubleheightavg1=176.2;4 intHeightavg2= (int) HeightAvg1;5 System.out.println (HEIGHTAVG1);6 System.out.println (HEIGHTAVG2);7 }8}
Web-android Engineer first form forced type conversion in -2-10 Java