Forced type conversions in Java
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
650) this.width=650; "src=" http://img.mukewang.com/53688a9a0001f73404330109.jpg "/>
Run Result: 650) this.width=650; "src=" http://img.mukewang.com/5359d73a00016cde00500033.jpg "/> can be seen by forcing type conversion to assign a value of 75.8 to After the int variable, the result is 75, 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!
Code:
public class helloworld{
public static void Main (string[] args) {
Double heightavg1=176.2;
int heightavg2= (int) heightAvg1;
System.out.println (HEIGHTAVG1);
System.out.println (HEIGHTAVG2);
}
}
Operation Result:
176.2
176
1. Coercion type conversion syntax: (data type) numeric value
2. Note the use of English symbols
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547644
Java Foundation---Forced type conversion in Java (eight)