The type conversions between variables mainly include automatic type conversions and forced type conversions.
1, automatic type conversion: When the size of the small data type and capacity of large data types to do the operation, the small capacity will automatically convert into large-capacity type.
"Knowledge points":
A) char,byte,short---> int---> Long---> Float---> Double---> String
Charc = ' a ';
Shorts = 12;
byteb = 125; //all operations between Char, short, and byte are automatically converted to int intS1 = C +s; intS2 = b +s; intS3 = C +b; LongL = 1000L; //the operation between int and long is automatically converted to a long LongL1 = S1 +l; floatf = 12.36f; //the operation between long and float is automatically converted to float floatF1 = L1 +F; DoubleD = 16952; //the operation between float and double is automatically converted to double DoubleD1 = f1 + D;
b) That is not a question that has been reflected.
Short A = 3; Compile normal
A = a + 3; Compile error
A + = 3; Compile correctly (reason reference coercion type conversion)
3 is also the default type int type, why the direct assignment to a nothing, and a+3 automatically converted to int type, and then the assignment is compiled wrong?
The problem key point is a constant at 3, which is a compilation error only if the assignment is followed by a variable. So our title is called the type conversion between variables.
int a = 1; int b = 2; byte b1 = 1 + 2; // compile correctly byte b2 = a + b; // compilation error (cannot convert from int to byte)
Then the constant operation also summarizes:
byte B1 = 1 + 126; // compile correctly byte b2 = 1 + 127; // compilation error (cannot convert from int to byte) int A = 1 + 1.2; // compilation error (cannot convert from double to int) String s2 = 12 + ""; // compile correctly
(1) 127 in the range of byte, compile correctly, and 128 exceeds the range of byte, compile error.
(2) 1 + 1.2 = 2.2,2.2 is a double type that exceeds the range of int, compilation error.
(3) + "=" (int + string = string), who and string are automatically converted to string.
Summary: Do a constant operation to see the numerical range of the result. You can't simply look at the type range.
C) Written questions:
String a = "abc"; char c = ' a '; // the ASCII code of A is int i =n; System.out.println (a+c+i); // Abca12 System.out.println (C+i+a); // 109abc System.out.println (a+i+c); // abc12a
2. Coercion type conversion: When the capacity is large to convert to small capacity, the coercion type conversion is required. Use the form: (type) to do the casting.
"Knowledge points":
A) Problem of forced type conversion: loss of precision caused
int i =n; byte by = (byte) I; System.out.println (by);
Output: 128
b) operator operations such as +=,-=,*= are automatically coerced to type conversions.
For example, the above case: A + = 3; It's actually equivalent to short a = (short) a+3; so it's definitely not compiling errors.
And then brother combed the Java Knowledge points--type conversion between variables (iv)