类型转换注意事项

类型转换注意事项

public class TypeSwitch {
   public static void main(String[] args) {
       int i = 128;
       byte b = (byte)i; //内存溢出
       //强制转换 (类型) 变量名 高->低
       //自动转换   低->高
       /*
       1.不能对布尔值进行转换
       2.不饿能把对象类型转换为不相干的类型
       3.把高容量转换到低容量是,强制转换
       4.转换的时候可能存在内存溢出,或存在精度问题
        */
       int money = 10_0000_1000;
       int years = 20;
       int total = money*years;  //计算的时候溢出了
       long tatal2 = money*years; //转换之前已经存在问题
       long total3 = money*((long)years); //先把一个数抓换为long
       System.out.println(total3);
  }
}
posted @ 2020-06-30 14:29  邱大将军  阅读(66)  评论(0编辑  收藏  举报