类型转换
public class Demo03 {
public static void main(String[] args) {
//类型转换 优先级👇
//抵------------------------高
//byte,short,char->int->long->float->double(小数的优先级一定大于整数)
//运算中 不同类型的数据线转化为同一类型,然后进行运算
int i =128;
byte b=(byte)i;//强制转换--->高转换到低(类型)变量名
System.out.println(i);
System.out.println(b);//内存溢出 (byte最大表示的数为127)
//自动转换--> 低到高
int a = 128;
double h = a;
System.out.println(a);
System.out.println(h);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.大容量到低容量的时候强制转换
4.转换的时候可能遇到内存溢出或者精度问题
*/
System.out.println("===========================");
System.out.println((int)23.7);//double
System.out.println((int)-45.89f);//浮点
System.out.println("===========================");
char c ='a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
public class Demo04 {
public static void main(String[] args) {
//操作比较大的数的时候 注意溢出问题
int money = 10_0000_0000;//数字之间可以用下划线分割
System.out.println(money);
int years = 20;
int total=money*years;
System.out.println(total);//-1474836480 计算的时候假溢出了
/* 在money和years计算的时候 因为money和years都是int类型
* 所以结果已经是int类型了 默认就是int类型了 转换之前已经存在问腿*/
long total1=money*years;
System.out.println(total1);
long total2=money*(long)years;
System.out.println(total2);
}
}