-
JAVA是强类型语言,所以要进行有些运算的时候,需要用到类型转换。
-
低到高排列
byte,short,char--> int -->long -->float-->double
-
运算中,不同类型转换为同一类型,然后再进行运算
//强制转换 高到低转换 (类型)变量名 低到高转换为自动转换
列 //高容量转低容量 (int转byte)
int i=70;
byte b=(byte)i; //byte范围-128-127
System.out.println(i);//输出 70
System.out.println(b);//输出 70
//低容量转高容量 (char转int)
char z='a';//字符的本质还是数字,a为97
int x=z+1;
System.out.println(x);//输出 98
float n=20.3F;
double m=66.666;
System.out.println((int)n);//输出 20 小数点后边的数字被消除
System.out.println((int)m);//输出 66 小数点后边的数字被消除
//溢出问题
int money = 10_0000_0000;
int years = 20;
int total = money*years;
System.out.println(total);//输出 -1474836480
long total2 = money*years;
System.out.println(total2);//输出 -1474836480 问题已经出在前边,所以结果不对
long total3 = money*((long)years);
System.out.println(total3);//输出 2000000000
或
long total2 = ((long)money)*years;//要在进行计算前转换为long



