数据类型转换

数据类型转换

隐式

​ 代码不需要进行特殊处理,自动处理
​ 规则:数据范围从小到大

1
2
3
4
5
6
7
8
9
10
11
12
public class test006 {
public static void main(String[] args) {
/*从小到大隐式转换*/
byte a = 1;
short c = 3;
int d = 4;
long e = 5;
e = d = c = a;
System.out.println(e);

}
}

显示

​ (类型名) 数据
​ (类型名)(数据)
​ 可能发生数据溢出和精度损失
​ byte/char/short整数运算默认会自动提升为int类型
​ boolean类型不能发生数据类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class test006 {
public static void main(String[] args) {
/*显示强制转换*/
byte a = 2;
int b = 4;

a = (byte)(b);
System.out.println(a);
b = 20;
a = (byte)b;
System.out.println(a);

}
}