类型转换注意点:(ms)

类型转换注意点:(ms)

public class Demo02 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;
        System.out.println(i); // 128
        // byte的取值是-128~127 , 因此发生内存溢出
        System.out.println(b); // -128
        System.out.println("=============");
        // JDK7新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);
        // 操作比较大的数的时候,注意溢出问题
        int year = 20;
        int total = money*year;
        System.out.println(total); // -1474836480,计算的时候溢出了
        long total2 = money*year;
        System.out.println(total2); // -1474836480 int*int 默认是int,转换之前已经存在问题了
        long total3 = money*((long)year);
        System.out.println(total3); // 20000000000 先把一个数转换为long


        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d); // 123123123123264 long 只要运算中有数值为long或double类型,那么结果也为该类型
        System.out.println(b+c+d); // 141 int 没有的话则默认int类型
        System.out.println(c+d); // 18 int 没有的话则默认int类型
    }
}

posted @ 2020-04-22 22:21  阿亮在努力  阅读(92)  评论(0)    收藏  举报