Home >Java >javaTutorial >Examples and analysis of automatic type conversion mechanism in Java
Automatic type conversion is also called implicit type conversion
Expression The data type is automatically promoted
All byte, short and char values will be promoted to int type.
If an operand is of type long, the calculation result is of type long;
If an operand is of type float, the result of calculation is of type float;
If an operand is of type float It is of type double, and the calculation result is of type double.
int values can be assigned to long, float, and double type variables, but cannot be assigned to byte, short, and char type variables
The same is true for function parameter passing
Of course, in the case of function overloading, the java compiler will automatically select the most The matching function is called
All types with a length lower than int (byte, short, char) After the operation, the result will be promoted to int type
Of course there is the following situation. This situation is because when we perform the assignment operation, the java compiler The processor can clearly know whether the result of the operation exceeds the value range of byte or short, so byte a = 1 1; does not report an error. The reason why the above byte c = a b; compilation error is because a and b are both variables, and the compiler does not know whether the result of the addition will exceed the value range of byte, so the compiler promotes the result to int type.
To summarize:
When the compiler clearly knows that the result of an integer operation does not reach the representation of int range, the operation result of byte, short or char type will not be automatically promoted to int type
When the compiler clearly knows or does not know whether the integer operation result reaches the representation range of int , the compiler will automatically convert the result of the operation into int, even if it is originally of type byte, short or char.
Answer: Assignment | When the data types of both sides are inconsistent during operation, Type conversion will occur
As follows:
public class TypeTest { public static void main(String[] args){ // 运算时发生的隐式类型转换,两整数相除得到的还是一个整数 byte a = 3; byte b = 4; int num = a + b; System.out.println(num); // 7 // 赋值时发生的隐式类型转换 int ch = '0'; System.out.println(ch); // 48 // 运算时发生的强制类型转换 byte a1 = 12; byte a2 = 12; byte num1 = (byte)(a1 + a2); System.out.println(num1); // 24 // 赋值时发生的强制类型转换 short b3 = 1234; byte a3 = (byte) b3; System.out.println(a3); // -46 } }
Run screenshot:
##Type conversion classificationRules: From small to large, low byte is automatically promoted to high byte
Sequence:
byte(1 byte) – > short( 2 bytes) -- > int (4 bytes) – > long (8 bytes) --> float (4 bytes) – > double (8 bytes) char (2 bytes) -- > int (4 bytes) – > long (8 bytes) --> float (4 bytes) – > double (8 bytes)Drawing analysis:
Code display:public class TypeDemo { public static void main(String[] agrs){ // byte -- > short byte b1 = 127; short s1 = b1; System.out.println(s1); // 127 // short -- > int short s2 = 30000; int i = s2; System.out.println(i); // 30000 // int -- > long int num = 2100000000; long lg = num; System.out.println(num); // 2100000000 // long -- > float long lg1 = 200000000000000L; float f1 = lg1; System.out.println(f1);// 2.00000001E14 // float -- > double float f2 = 3.14f; double d1 = f2; System.out.println(d1); // 3.140000104904175 // char -- > int char ch = 'a'; int i1 = ch ; System.out.println(i1); // 97 // char -- > long char ch2 = 'b'; long lg2 = ch2; System.out.println(lg2); // 98 // char -- > double char ch3 = 'c'; double dou = ch3; System.out.println(dou); // 99.0 // char -- > float char ch4 = 'd'; float f3 = ch4; System.out.println(f3); // 100.0 } }Running screenshot:
Note:
byte and short cannot be converted to charCode display:public class TypeDemo2 { public static void main(String[] agrs){ // byte -- > char byte bt = 127; char ch = bt; System.out.println(ch); // short -- > char short sh = 12; char ch2 = sh; System.out.println(ch2); } }Compilation error screenshot:
Although float is 4 bytes, float represents a larger data range than long. Explain that the size of the data range and the size of bytes are not necessarily relatedCode display:
public class TypeDemo3 { public static void main(String[] agrs){ long lg = 20000000000000L; float f1 = lg; System.out.println(f1); // 1.99999997E13 } }Running screenshot: boolean type cannot Participating in type conversionCode display:
public class TypeDemo4 { public static void main(String[] agrs) { boolean flag = 12; int flag1 = flag; System.out.println(flag1); } }Compilation error screenshot: Forced type conversion (explicit type conversion)
Rules: Manually cast from large to small, high byte to low byte
Sequence:
double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > short (2字节)-- > byte(1字节)
double(8字节) – > float(4字节) – > long(8字节) – > int(4字节) – > char(2字节)
画图分析:
(掌握)格式:目标数据类型 变量名 = (目标数据类型) 变量 | 常量;
代码展示:
public class TypeDemo5 { public static void main(String[] agrs){ // float -- > long // final float PI = 3.14f; // long num = (long) PI; // 3 // float little = 3.14f; // long num = (long)little; // 3 long num = (long)3.14f; System.out.println(num);// 3 // double -- > float // double dou = 3.14; // float little1 = (float)dou; // 3.14 // float little1 = (float) 3.14d; // 3.14 final double dou = 3.14; float little1 = (float)dou; System.out.println(little1); // 3.14 // long -- > int // long num1 = 2000000000000L; // int num2 = (int)num1; // -1454759936 // int num2 = (int)2000000000000L; // -1454759936 final long num1 = 2000000000000L; int num2 = (int)num1; System.out.println(num2); // -1454759936 // int --> short // int num3 = 12; // short num4 = (short)num3; // 12 // short num4 = (short)40000; // -25536 final int num3 = 60; short num4 = (short)num3; System.out.println(num4); // 60 // short -- > byte final short sh = 12345; byte bt = (byte)sh; System.out.println(bt); // 57 short sh2 = 78; bt = (byte) sh2; System.out.println(bt); // 78 } }
运行截图:
注意:
强制类型转换有数据丢失,一般不建议使用
代码展示:
public class TypeDemo6 { public static void main(String[] agrs) { short a = 1245; byte b = (byte)a; System.out.println(b); } }
运行截图:
The above is the detailed content of Examples and analysis of automatic type conversion mechanism in Java. For more information, please follow other related articles on the PHP Chinese website!