类型转换
强制转换 (类型)变量名 高–低
int i=128;
byte b=byte(i);//内存溢出
print
128
-128
自动转换 低–高
注意:1.不能对布尔值进行转换
2.不能对对象类型转换为不相干的类型
3.在把高容量转换为低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题!
char c='a';
int d=c+1;
System.out.println(d);
System.out.println((char)d);
print:
98
b
int money=10_0000_0000;//溢出
int years=20;
int total=money*years;
long total2=money*years;
System.out.println(total);
System.out.println(total2);
long total3=money*((long)years);
System.out.println(total3);
print
-1474836480 //溢出
-1474836480 //乘法那就已经溢出
20000000000 //money和years乘法那已经是long类型,不会溢出