public class Demo03 {
//数据类型低类型---------------------------------高类型
//byte,short,char -< int -< long -< float -< double
//强制转换 从高类型转低类型 double->float
//自动转换 从低转换高类型 int-<double
public static void main(String[] args) {
int a =128;
byte b = (byte)a; //内存溢出
double c = a ; //自动转换
System.out.println(a);
System.out.println(b);
System.out.println(c);
//注意:
// 布尔值不可转换
// 不能把数据类型转换为不相干的类型
//高转低需要强制转换,可能存在内存溢出或精度问题
System.out.println((int)15.5); //15精度问题
System.out.println((int)-32.254f); //-32精度问题
int money = 10_0000_0000;
int years = 20;
int total = money*years;//-1474836480,计算时溢出
long total2 = money*years;//-1474836480,默认是Int类型,转换之前问题 已经存在
System.out.println(total2);
long total3 =money*((long)years); //20000000000 先将其中一个数转为long类型,底层执行自动转换
System.out.println(total3);
}
}