类型转换
低·······································································>高
(低)byte,short,char ·> int ·> long ·> float ·> double(高)
public class demo05 {
public static void main(String[] args) {
/*
强制转换 (类型)变量名 高到低
自动转换 低到高
*/
int i = 128;
byte b = (byte)i; //byte范围 -128~127(内存溢出)
double c = i;
System.out.println(i);
System.out.println(b);
System.out.println(c);
}
}
- 输出结果
public class demo05 {
public static void main(String[] args) {
/*
数字之间可以使用下划线
*/
int money = 10_0000_0000;
int years = 20;
int total = money*years; //计算溢出
long total2 = money*years; //默认是int
long total3 = money*((long)years); //先把一个数进行转换
System.out.println(total);
System.out.println(total2);
System.out.println(total3);
}
}
- 输出结果
- 结论
- 布尔值不能转换
- 不能把对象类型转换为不相干的类型
- 在把高容量转换为低容量时,强制转换
- 转换的时候存在内容溢出或者精度问题