什么时候会发生类型转换?
答: 赋值 | 运算时 ,两边数据类型不一致时就会发生类型转换
如下:
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
}
}
运行截图:
类型转换分类:
- 自动类型转换
- 强制类型转换
自动类型转换(隐式类型转换)
规则:从小到大 ,低字节向高字节自动提升
顺序:
-
byte(1字节) – > short(2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)
-
char (2字节)-- > int(4字节) – > long(8字节) --> float(4字节) – > double(8字节)
画图分析:
代码展示:
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 ch1 = 'b';
long lg2 = ch1;
System.out.println(lg2); // 98
// char -- > double
char ch2 = 'c';
double dou = ch2;
System.out.println(dou); // 99.0
// char -- > float
char ch3 = 'd';
float f3 = ch3;
System.out.println(f3); // 100.0
}
}
运行截图:
注意:
-
byte、short不能和char进行相互转换
代码展示:
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 ch1 = sh; System.out.println(ch1); } }
编译错误截图:
-
float虽然是4个字节,但是float比long表示的数据范围更大。说明数据范围的大小和字节的大小不一定相关
代码展示:
public class TypeDemo3 { public static void main(String[] agrs){ long lg = 20000000000000L; float f1 = lg; System.out.println(f1); // 1.99999997E13 } }
运行截图:
-
boolean类型不能参与类型转换
代码展示:
public class TypeDemo4 { public static void main(String[] agrs) { boolean flag = 12; int flag1 = flag; System.out.println(flag1); } }
编译错误截图:
强制类型转换(显式类型转换)
规则:从大到小,高字节向低字节手动强制转换
顺序:
-
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 sh1 = 78;
bt = (byte) sh1;
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); } }
运行截图: