Automatic type conversion in Java
In Java programs, it is often necessary to convert data between different basic data types. For example:
650) this.width=650; "src=" http://img.mukewang.com/5359d0c70001ffe202530066.jpg "/> in code int type variable Score1 can be directly for the double variable Score2 completion of the assignment operation, The result is: 650) this.width=650; src= http://img.mukewang.com/ 5359d10d0001d9c500540024.jpg "/> 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
650) this.width=650; "src=" http://img.mukewang.com/53688a3200014fc803800109.jpg "/>
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
650) this.width=650; "src=" http://img.mukewang.com/53688a460001539403740086.jpg "/>
Code:
public class helloworld{
public static void Main (string[] args) {
Double avg1=78.5;
int rise=5;
Double avg2=avg1+rise;
System.out.println ("Average test score:" +AVG1);
SYSTEM.OUT.PRINTLN ("Adjusted mean score:" +AVG2);
}
}
Operation Result:
Average Test score: 78.5
Adjusted average score: 83.5
1. Note The conditions for automatic type conversion Oh, theint type can be automatically converted to a double type, but the double type cannot be automatically converted to the INT type
2.5th line reference: Double avg2 = Avg1 + rise;
This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547643
Java Foundation---Automatic type conversion in Java (vii)