Grammar:
A. Automatic type conversion: Under certain conditions, the system auto-complete type conversion is also called the implicit conversion (implicit casting)
A. Two types of compatibility
B. Destination type is greater than source type
C. For an expression, if one operand is of type double, the entire expression can be promoted to type double
B. Coercion type conversion (Explicit Casting): Under certain conditions, the programmer manually completes the type conversion
A. Two types of compatibility
B. Generic target type small fish source type
IMPORTANT: 1. Forcing type conversions will lose precision
Example: 1. Automatic type conversion:
int i = 10;
Double d = i;
2. Force type conversions:
Double d = 10.0;
int i = (int) d;
Example code:
1 PackageCom.sanlianxueuan;2 3 Public classDemo03 {4 5 /**6 * Conversion of data types7 */8 Public Static voidMain (string[] args) {9 //Automatic type conversion (implicit conversion)Ten byteNUM1 = 10; One intnum2 =NUM1; A System.out.println (num2); - LongNUM3 =num2; - System.out.println (num3); the - floatf = 10.5f; - DoubleD =F; - System.out.println (d); + - intNUM4 = 100; + floatNUM5 =num4; A System.out.println (NUM5); at - CharC1= ' a '; - intNUM6 = C1;//character assigned value to shaping variable - System.out.println (NUM6); -System.out.println (c1+10); -System.out.println ((Char) (c1+10)); in - to //Forcing type conversions (explicit conversions) + intNUM7 = 10; - ShortNUM8 = ( Short) Num7; the System.out.println (NUM8); * $ floatNUM9 = (float) 10.25;Panax Notoginseng System.out.println (NUM9); - the intNUM10 = 65; + CharNUM11 = (Char) NUM10; A System.out.println (NUM11); the + intNUM12 = 10; - DoubleNum13 =10.6; $ floatres = (float) (num12+num13); $ System.out.println (res); - - /*Summary: Byte<short (char) <int<long<float<double*/ the - }Wuyi the}
Java Data type conversions