类型转换及拓展

public class TypeConversion {
    public static void main(String[] args) {
        /**
         *  低  -----   高
         *   byte short char int long float double
         * */

        int a = 128;
        byte b = (byte) a;//-128
        //强制转换的时候,可能会内存溢出

        /**
         *  拓展
         * */

        //操作较大的数的时候,注意溢出的问腿
        int money = 1000000000;
        int years = 20;
        int total = money * years;//-1474836480,计算的时候溢出了
        System.out.println(total);
        //解决办法
        long total1 = money * (long)years;//先把一个数转为long,则默认用long计算了
        System.out.println(total1);//20000000000

    }
}

posted @ 2022-10-19 00:05  Always_0708  阅读(23)  评论(0)    收藏  举报